Data Structures & AlgorithmsVery hardICPC regional · Codeforces Div2 D

Stalls are at positions [1, 2, 8, 4, 9, 15, 17] on a line. You must place C = 4 cows in distinct stalls to maximise the MINIMUM distance between any two cows. What is that maximum minimum distance?

  1. A. 3
  2. B. 8
  3. C. 4
  4. D. 2

Correct answer: A. 3

Binary search on the answer d: check whether greedily placing each cow at the first stall at least d beyond the previous one fits all cows. The check is monotone, so the largest feasible d is the answer. Dividing the span evenly by the cow count ignores where the stalls actually are.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic