Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT

From the numbers [1, 9, 4, 7, 3, 12, 8, 5], form as many DISJOINT pairs as possible where the two numbers in each pair differ by at most 2 (each number is used at most once). What is the maximum number of pairs?

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

Correct answer: A. 3

Sort and pair neighbours greedily: the closest pairs in sorted order are the ones that can be paired, and skipping an element that cannot pair with its successor is safe. Taking the first available partner in input order can block a better pairing.

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

Exams that ask this topic