Data Structures & AlgorithmsHardTCS NQT · Codeforces Div2 B

From the numbers [1, 2, 3, 5, 6, 8, 9, 10], what is the size of the largest subset in which any two chosen numbers differ by at least 2?

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

Correct answer: A. 5

Sort the values, then take a number whenever it is at least 2 more than the last one taken. Taking every other element by position is a different rule and can fail when gaps are uneven.

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

Exams that ask this topic