Maximize the Smallest Separation

Quick Overview

Maximize the Smallest Separation. There are baskets at the distinct coordinates in position. Place exactly m balls, at most one in each basket. The separation of two balls is the absolute difference of their coordinates.

Maximize the Smallest Separation

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem There are baskets at the distinct coordinates in `position`. Place exactly `m` balls, at most one in each basket. The separation of two balls is the absolute difference of their coordinates. Return the greatest achievable minimum pairwise separation. ```cpp class Solution { public: int maxDistance(vector<int>& position, int m); }; ``` ### Examples **Example 1:** ```text position = [2,5,8,11], m = 3 Output: 3 Explanation: Baskets 2, 5, 11 give a minimum gap of 3. A gap greater than 3 allows at most two balls. ``` **Example 2:** ```text position = [10,3,7], m = 2 Output: 7 Explanation: Use the extreme coordinates 3 and 10. ``` ### Constraints - `n == position.length`, with `2 <= n <= 10^5`. - Coordinates are distinct integers in `[1, 10^9]`. - `2 <= m <= n`. ### Clarifying Questions Each selected basket holds one ball. Only the maximum separation value is returned, so several optimal placements do not create multiple answers. ```hint Inspect the structure What must remain true about every selected pair when exactly `m` baskets are occupied? ``` ### Reasoning checkpoints - Show that the reported minimum separation can be achieved with exactly `m` distinct baskets. - Rule out every larger minimum separation and explain resource use at the stated coordinate and input-size limits. ### Optional extensions If the ball coordinates themselves were also requested, how would you recover one placement after finding the optimum?

Overview: Maximize the Smallest Separation. There are baskets at the distinct coordinates in position. Place exactly m balls, at most one in each basket. The separation of two balls is the absolute difference of their coordinates.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

There are baskets at the distinct coordinates in position. Place exactly m balls, at most one in each basket. The separation of two balls is the absolute difference of their coordinates. Return the greatest achievable minimum pairwise separation.

class Solution {
public:
    int maxDistance(vector<int>& position, int m);
};

Examples

Example 1:

position = [2,5,8,11], m = 3
Output: 3
Explanation: Baskets 2, 5, 11 give a minimum gap of 3. A gap greater than 3 allows at most two balls.

Example 2:

position = [10,3,7], m = 2
Output: 7
Explanation: Use the extreme coordinates 3 and 10.

Constraints

  • n == position.length , with 2 <= n <= 10^5 .
  • Coordinates are distinct integers in [1, 10^9] .
  • 2 <= m <= n .

Clarifying Questions Guidance

Each selected basket holds one ball. Only the maximum separation value is returned, so several optimal placements do not create multiple answers.

Reasoning checkpoints

  • Show that the reported minimum separation can be achieved with exactly m distinct baskets.
  • Rule out every larger minimum separation and explain resource use at the stated coordinate and input-size limits.

Optional extensions

If the ball coordinates themselves were also requested, how would you recover one placement after finding the optimum?