Maximize Score of Numbers in Ranges - Problem

You are an optimization strategist tasked with making the smartest choices from constrained ranges. Given an array of starting positions start and a range distance d, you have n intervals where the i-th interval is [start[i], start[i] + d].

Your mission: Choose exactly one integer from each interval such that the minimum absolute difference between any two chosen numbers is as large as possible. This minimum difference is your "score" - and you want to maximize it!

Think of it as: You're placing guards at strategic positions within their assigned zones, and you want to ensure the closest two guards are as far apart as possible for maximum coverage.

Example: If start = [1, 0, 9] and d = 2, your intervals are [1,3], [0,2], [9,11]. You might choose 2, 0, 10 giving you differences of |2-0|=2, |10-2|=8, |10-0|=10. The minimum is 2, which becomes your score.

Input & Output

example_1.py โ€” Basic case
$ Input: start = [1, 0, 9], d = 2
โ€บ Output: 2
๐Ÿ’ก Note: Intervals are [1,3], [0,2], [9,11]. We can choose 2 from [1,3], 0 from [0,2], and 10 from [9,11]. The differences are |2-0|=2, |10-2|=8, |10-0|=10. The minimum is 2, which is the maximum possible.
example_2.py โ€” Close intervals
$ Input: start = [2, 6, 13, 13], d = 5
โ€บ Output: 5
๐Ÿ’ก Note: Intervals are [2,7], [6,11], [13,18], [13,18]. We can choose 2, 7, 13, 18 giving minimum difference of 5 (between 2 and 7).
example_3.py โ€” Single interval
$ Input: start = [5], d = 3
โ€บ Output: 0
๐Ÿ’ก Note: With only one interval [5,8], we choose one number. Since there are no pairs, the minimum difference is undefined, but conventionally we return 0 for single element cases.

Constraints

  • 1 โ‰ค start.length โ‰ค 104
  • 0 โ‰ค start[i] โ‰ค 108
  • 0 โ‰ค d โ‰ค 104
  • All intervals have the same length d

Visualization

Tap to expand
Strategic Guard PositioningPatrol Zones (Intervals)Zone A [1,3]Zone B [0,2]Zone C [9,11]Optimal Guard PlacementGuard B at 0Guard A at 2distance = 2Guard C at 9distance = 7Why This is Optimalโ€ข Minimum distance between any two guards = 2โ€ข Cannot improve: Guard A cannot move left (outside zone)โ€ข Guard B cannot move right (would decrease minimum distance)Maximum Achievable Score: 2Binary Search Process:1. Try distance 5: Impossible2. Try distance 2: Possible3. Try distance 3: Impossible4. Answer = 2 โœ“
Understanding the Visualization
1
Zone Assignment
Each guard gets a patrol zone [start[i], start[i] + d] where they must be positioned
2
Distance Optimization
We want to position guards so the closest two are as far apart as possible
3
Greedy Placement
Sort zones and greedily place guards as far left as possible while maintaining minimum distance
Key Takeaway
๐ŸŽฏ Key Insight: The problem has a monotonic property - if we can achieve distance X, we can achieve any smaller distance. This makes binary search on the answer optimal, combined with greedy validation.
Asked in
Google 28 Amazon 22 Meta 15 Microsoft 12
28.5K Views
Medium Frequency
~25 min Avg. Time
874 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