InferenceAI

Enter at least two characters to search.

Start free

Spread the Sensors

hardBinary Search40 min50 XP

Sensors can be installed at the positions in spots (one per spot). Install exactly k sensors so that the smallest distance between any two sensors is as large as possible, and return that distance.

Requirements

  • Binary search on the answer: for a candidate distance dd, greedily check whether kk sensors fit.

Constraints

  • 2≤k≤n≤1052 \le k \le n \le 10^5; 0≤0 \le spots[i] ≤109\le 10^9

Examples

EXAMPLE WALKTHROUGHclassic

A guided view of the published example, not a trace of your code.

INPUT[[1,2,4,8,9],3]
TASKspread_the_sensors()
EXPECTED OUTPUT3

Start with the input values shown in this example.

Example 1 of 3

Input: spots = [1,2,4,8,9], k = 3

Output: 3

Explanation: classic

Input: spots = [1,2,3], k = 3

Output: 1

Explanation: tight

Input: spots = [0,1000000000], k = 2

Output: 1000000000

Explanation: two ends

Hint 1

Sort the spots. Feasibility of a distance d is monotonic: if d works, every smaller d works too.

Hint 2

To test d, place the first sensor at the first spot and each next one at the first spot at least d away.

Topics

DSABinary SearchPython