The torches on the wall
- O(n log n) · Hard
- Full plan
- Python
- JavaScript
- lists
- sorting
- decimals
Problem
No wall on the edge of the empire is ever left in the dark. This one is steps paces long, with torches planted at different points along it. The centurion wants them all burning with the same flame: the smallest one that leaves no stretch unlit.
Each torch lights reach paces to each side. You get torches, a list of integers between 0 and steps (at least one, in any order, and two can stand at the same point), and steps, an integer greater than 0. Return the smallest reach that lights the whole wall, from pace 0 to pace steps. It can end in .5: between two torches 5 paces apart, 2.5 is enough, because each one covers half. At the ends nobody helps: if the first torch stands at pace 4, it needs 4 to reach 0, and the same goes for the last one up to steps. With [12, 4, 9] and 20 steps you return 8, the distance from the last torch to the end.
Examples
The far end decides
[12, 4, 9], 20 → 8
Two torches 5 paces apart
[2, 7], 9 → 2.5
The gap in the middle decides
[0, 10, 4, 16], 16 → 3
Out of order
[9, 1, 5], 10 → 2
One- and two-digit paces
[40, 5, 12, 30], 45 → 9
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def torch_reach(torches, steps):
passJavaScript
function torchReach(torches, steps) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.