FastPrepAggressive Cows

Aggressive Cows

Amazon logoAmazonâ—Ź MediumFULLTIMEPHONE SCREEN

Problem statement

Given distinct integer stall positions stalls and an integer cows, place exactly cows cows in different stalls.

Return the largest possible value of the minimum distance between every pair of placed cows.

Function

aggressiveCows(stalls: int[], cows: int) → int

Examples

Example 1

stalls = [1,2,4,8,9]cows = 3return = 3

Placing cows at 1, 4, and 8 gives a minimum distance of 3, which cannot be improved.

Example 2

stalls = [10,1,2,7,5]cows = 3return = 4

After sorting, positions 1, 5, and 10 achieve minimum distance 4.

Example 3

stalls = [0,5]cows = 2return = 5

Both stalls must be used, so their distance is the answer.

Constraints

  • 2 <= stalls.length <= 10^5.
  • 2 <= cows <= stalls.length.
  • 0 <= stalls[i] <= 10^9.
  • All stall positions are distinct.

Source note: Source excerpt showing the reported interview task.

More Amazon problems

See Amazon hiring insights
Saved
public int aggressiveCows(int[] stalls, int cows) {
  // write your code here
}
Checking account…