Maximize Score of Numbers in Ranges - Problem

You are given an array of integers start and an integer d, representing n intervals [start[i], start[i] + d].

You are asked to choose n integers where the i-th integer must belong to the i-th interval. The score of the chosen integers is defined as the minimum absolute difference between any two integers that have been chosen.

Return the maximum possible score of the chosen integers.

Input & Output

Example 1 — Basic Case
$ Input: start = [4, 6, 8], d = 2
Output: 2
💡 Note: Intervals are [4,6], [6,8], [8,10]. We can choose integers 4, 6, 8 with minimum difference = min(|6-4|, |8-6|, |8-4|) = min(2, 2, 4) = 2
Example 2 — Same Start Points
$ Input: start = [2, 6, 8], d = 0
Output: 0
💡 Note: With d=0, each interval contains only one integer: [2,2], [6,6], [8,8]. We must choose 2, 6, 8 with minimum difference = min(|6-2|, |8-6|, |8-2|) = min(4, 2, 6) = 2
Example 3 — Overlapping Intervals
$ Input: start = [6, 13, 18], d = 10
Output: 3
💡 Note: Intervals are [6,16], [13,23], [18,28]. Optimal selection might be 6, 16, 19 giving minimum difference = min(|16-6|, |19-16|, |19-6|) = min(10, 3, 13) = 3

Constraints

  • 1 ≤ start.length ≤ 104
  • 0 ≤ start[i] ≤ 108
  • 0 ≤ d ≤ 109

Visualization

Tap to expand
Maximize Score: Select Integers from IntervalsInterval [4, 6]Interval [6, 8]Interval [8, 10]Choose: 4, 5, or 6Choose: 6, 7, or 8Choose: 8, 9, or 10Goal: Maximize minimum absolute difference between chosen integers468|6-4| = 2|8-6| = 2Selected: 4, 6, 8 → Minimum difference = min(2, 2, 4) = 2Maximum Score: 2
Understanding the Visualization
1
Input
Given intervals [4,6], [6,8], [8,10]
2
Process
Select one integer from each interval to maximize minimum difference
3
Output
Optimal selection gives maximum score = 2
Key Takeaway
🎯 Key Insight: Use binary search on the answer space to efficiently find the maximum achievable minimum difference
Asked in
Google 15 Amazon 12 Meta 8
18.5K Views
Medium Frequency
~35 min Avg. Time
580 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen