Maximize the minimum gap when choosing one integer from each interval

Quick Overview

Pick one integer from each of n intervals of equal length so that the smallest difference between any two picks is as large as possible. The problem tests binary search on the answer, a greedy feasibility check, and the argument for why sorting the intervals first is safe.

Maximize the minimum gap when choosing one integer from each interval

Company: Deshaw

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an integer array `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 the minimum absolute difference between any two integers that have been chosen. Return the maximum possible score of the chosen integers. ### Function Signature ```python def max_possible_score(start: list[int], d: int) -> int: ``` ### Rules - Intervals are closed, so both `start[i]` and `start[i] + d` may be chosen. - The score is the minimum of `|a_i - a_j|` over all pairs of different indices `i != j`. If two chosen integers are equal, the score is `0`. - `start` is not guaranteed to be sorted, and several intervals may be identical. ### Constraints - `2 <= len(start) <= 10^5` - `0 <= start[i] <= 10^9` - `0 <= d <= 10^9` ### Examples ```text Input: start = [6, 0, 3], d = 2 Output: 4 ``` The intervals are [6, 8], [0, 2] and [3, 5]. Choosing 8, 0 and 4 gives pairwise differences 8, 4 and 4, so the score is 4. A score of 5 is impossible: with 0 or more from [0, 2], the next pick must be at least 5, and then [6, 8] would need a value of at least 10. ```text Input: start = [2, 6, 13, 13], d = 5 Output: 5 ``` Choose 2, 7, 13 and 18. The closest pair is 5 apart. The two identical intervals [13, 18] can hold two numbers at most 5 apart, so no choice scores higher. ```text Input: start = [0, 0, 0], d = 10 Output: 5 ``` All three numbers come from [0, 10]. Choosing 0, 5 and 10 gives a score of 5. Three numbers in a range of width 10 have two gaps that add up to at most 10, so the smaller gap cannot exceed 5.

Overview: Pick one integer from each of n intervals of equal length so that the smallest difference between any two picks is as large as possible. The problem tests binary search on the answer, a greedy feasibility check, and the argument for why sorting the intervals first is safe.

Deshaw logo
Deshaw
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an integer array 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 the minimum absolute difference between any two integers that have been chosen.

Return the maximum possible score of the chosen integers.

Function Signature

def max_possible_score(start: list[int], d: int) -> int:

Rules

  • Intervals are closed, so both start[i] and start[i] + d may be chosen.
  • The score is the minimum of |a_i - a_j| over all pairs of different indices i != j . If two chosen integers are equal, the score is 0 .
  • start is not guaranteed to be sorted, and several intervals may be identical.

Constraints

  • 2 <= len(start) <= 10^5
  • 0 <= start[i] <= 10^9
  • 0 <= d <= 10^9

Examples

Input: start = [6, 0, 3], d = 2
Output: 4

The intervals are [6, 8], [0, 2] and [3, 5]. Choosing 8, 0 and 4 gives pairwise differences 8, 4 and 4, so the score is 4. A score of 5 is impossible: with 0 or more from [0, 2], the next pick must be at least 5, and then [6, 8] would need a value of at least 10.

Input: start = [2, 6, 13, 13], d = 5
Output: 5

Choose 2, 7, 13 and 18. The closest pair is 5 apart. The two identical intervals [13, 18] can hold two numbers at most 5 apart, so no choice scores higher.

Input: start = [0, 0, 0], d = 10
Output: 5

All three numbers come from [0, 10]. Choosing 0, 5 and 10 gives a score of 5. Three numbers in a range of width 10 have two gaps that add up to at most 10, so the smaller gap cannot exceed 5.