Data Structures & AlgorithmsModerateICPC regional · Amazon SDE OA

From [1, 2, 2, 3, 5, 6, 6], what is the maximum number of disjoint pairs where the two values differ by at most 1?

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

Correct answer: C. 3

Sort the values and pair neighbours greedily, skipping a value that cannot pair with its successor. The disjoint pairs (1,2), (2,3) and (5,6) give three.

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

Exams that ask this topic