Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Aggressive Cows

Medium Acceptance 70.00% Points 30.00

You are given an array of integers stalls[] representing the positions of stalls on a number line and an integer k representing the number of cows. Your task is to place the cows in these stalls such that the minimum distance between any two cows is as large as possible. Each stall can hold only one cow. Return the maximum possible minimum distance between any two cows.

Examples
Example 1

Example 1

Input: stalls = [1,2,4,8,9], k = 3

Output: 3

Explanation: The cows can be placed at positions 1, 4, and 8. The distances between cows are 3 and 4. The minimum distance is 3, which is the maximum possible.

Example 2

Example 2

Input: stalls = [1,3,5,7,9], k = 3

Output: 4

Explanation: The cows can be placed at positions 1, 5, and 9. The distances between cows are 4 and 4. The minimum distance is 4, which is the maximum possible.

Example 3

Example 3

Input: stalls = [10,20,30,40], k = 2

Output: 30

Explanation: The cows can be placed at positions 10 and 40. The distance between the cows is 30, which is the maximum possible minimum distance.

Hints
Hint 1
N/A
Constraints
  • 2 = stalls.length = 105
  • 1 = stalls[i] = 10?
  • 2 = k = stalls.length
Companies
Walmart
Topics
Binary Search
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions