Data Structures & AlgorithmsHardCodeforces Div2 B · TCS NQT

The sorted numbers [1, 3, 6, 7, 10, 11, 15] must be split into the fewest CONTIGUOUS groups so that in each group the largest minus the smallest is at most 2. What is the minimum number of groups?

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

Correct answer: C. 4

Greedy from the left: start a group at the smallest unassigned number and extend while the range stays within D. Starting groups at fixed positions (blocks of equal size) fails when gaps between values are uneven.

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

Exams that ask this topic