Maximize Minimum Distance
Updated
Problem statement
Given a set of n distinct points on the x-axis, choose k of them such that the minimum distance between any two chosen points is as large as possible. Find this maximum possible minimum distance.
Function
maximizeMinimumDistance(x: int[], k: int) → int
Complete the function maximizeMinimumDistance in the editor below.
maximizeMinimumDistance has the following parameters:
int x[n]: the x-coordinates of pointsint kthe number of points to choose
Returns
int: the maximum possible minimum distance between any 2 of the chosen points
Examples
Example 1
x = [1, 4, 2, 9, 8]k = 3return = 3In the optimal solution, one of the possible selection of points is {1, 4, 8}. Here,
- The distance between 1 and 4 = abs(1 - 4) = 3
- The distance between 1 and 8 = abs(1 - 8) = 7
- The distance between 4 and 8 = abs(4 - 8) = 4
The minimum amongst them is 3, which is the maximum possible.
Constraints
- 2 ≤ n ≤ 10^5
- 0 ≤ x[i] ≤ 10^9
- 2 ≤ k ≤ n
- All points are at distinct x-coordinates.
More IBM interview questions
- Missing IntervalsOA · Seen Nov 2026
- Count Peaks and ValleysOA · Seen Nov 2026
- Last Object Exit TimeOA · Seen Nov 2026
- Count Strictly Increasing Contiguous WindowsOA · Seen Nov 2026
- Total Each Letter's Count in an Encoded StringOA · Seen Nov 2026
- Count Servers With No Requests in a Time WindowOA · Seen Nov 2026
- Alternate Odd and Even Numbers with Two GoroutinesPHONE SCREEN · Seen Nov 2026
- Longest Substring Without Repeating CharactersPHONE SCREEN · Seen Nov 2026
- Maximum Easy Tasks After RearrangingOA · Seen Nov 2026
- Longest Palindromic SubstringPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Server Upgrade PlanningWells Fargo · OAWells Fargo interview questions
- Find Minimum TimeAlphaSense · OA
- Find Minimum TimeBlackRock · OABlackRock interview questions
- Maximize Pipeline Throughput Under a Scaling BudgetMicrosoft · OAMicrosoft interview questions
- Rock JumpingIMC Trading · OAIMC Trading interview questions
- Minimum Robot and Human Fulfillment TimeAmazon · OA
- Minimize Maximum Group DifferenceDRW · OADRW interview questions
- Array NullificationQRT · OA
- Minimum HeightSnowflake · OASnowflake interview questions
- Pipeline Throughput Maximization Under BudgetUber · OAUber interview questions