Data Structures & AlgorithmsVery hardICPC regional · Amazon SDE OA

Five stalls are at positions 1, 2, 4, 8, 9 on a line. Three cows are placed in distinct stalls so that the minimum distance between any two cows is maximised. What is that maximum minimum distance?

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

Correct answer: C. 3

Binary search on the distance d and check greedily whether 3 cows fit with gaps at least d. Stalls 1, 4 and 8 give minimum gap 3; a gap of 4 is impossible, so the answer is 3.

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

Exams that ask this topic