FastPrepMaximize the Minimum Distance Between Robots
Problem · Array

Maximize the Minimum Distance Between Robots

Learn this problem
MediumAmazon logoAmazonFULLTIMEONSITE INTERVIEW
See Amazon hiring insights

Problem statement

A factory line has candidate robot stations at the distinct integer coordinates in positions. Choose exactly robots stations.

The separation of a placement is the minimum absolute distance between any two selected stations. Return the largest separation that can be achieved.

Function

maximizeMinimumRobotDistance(positions: int[], robots: int) → int

Examples

Example 1

positions = [1,2,8,12,17]robots = 3return = 7

Stations 1, 8, and 17 give adjacent gaps 7 and 9, so the minimum is 7.

Example 2

positions = [5,9]robots = 2return = 4

Both stations must be selected, and their distance is 4.

Example 3

positions = [0,3,4,7,10]robots = 4return = 3

Selecting 0, 3, 7, and 10 achieves a minimum gap of 3.

Constraints

  • 2 <= positions.length <= 100000.
  • 2 <= robots <= positions.length.
  • 0 <= positions[i] <= 10^9.
  • All positions are distinct.

More Amazon problems

drafts saved locally
public int maximizeMinimumRobotDistance(int[] positions, int robots) {
  // write your code here
}
positions[1,2,8,12,17]
robots3
expected7
checking account