Data Structures & AlgorithmsHardCodeforces Div2 D · TCS NQT

A pair (a, b) can follow pair (c, d) in a chain when b < c. The pairs are [(5, 24), (39, 60), (15, 28), (27, 40), (50, 90), (1, 2), (3, 4), (6, 9), (10, 11)]. What is the length of the longest chain you can form? You may skip pairs, and pairs need not be used in the given order.

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

Correct answer: A. 6

Sort the pairs by their SECOND value and greedily take each pair whose first value is larger than the last taken second value. Sorting by the first value and then taking greedily fails, because a pair with a small second value should be preferred.

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

Exams that ask this topic