A street festival has a list of parking spots along one straight road, given as positions in metres. It wants to park k food trucks on those spots so that queues do not mix: the smallest distance between any two trucks should be as large as possible.
Write two functions that take spots, an array of positions in any order, and the number of trucks k:
maxMinGap(spots, k) returns the largest possible smallest distance between two trucks.
placeWithMaxGap(spots, k) returns { gap, positions }: that distance, and the positions of the trucks, sorted ascending, chosen greedily from the left: the first truck takes the smallest spot, and each next truck takes the first spot at least gap past the previous truck, until all k are placed.
Rules:
Each spot holds at most one truck. Two spots may have the same position; then the answer can be 0.
If spots is not an array of safe integers, throw a TypeError. If k is not an integer from 2 to spots.length, throw a RangeError.
Do not change spots.
There can be 200,000 spots with positions up to 1,000,000,000 apart. Trying every distance, or every pair of spots, will not finish in time.