Data Structures & AlgorithmsVery hardICPC regional · GATE CS

Intervals [(1, 3), (2, 5), (4, 6), (3, 4), (6, 8), (5, 7)] are closed integer ranges. Choose as many as possible so that every integer point is covered by at most 2 of the chosen intervals. What is the maximum number chosen?

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

Correct answer: A. 4

Subset enumeration on six intervals is the reference; a sweep with a multiset of current overlaps and an exchange argument gives the same answer at scale. Sorting by end time and taking any interval that does not overload a point is the greedy for the 'at most 1' case; the 'at most 2' case needs the overlap count, not just disjointness.

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

Exams that ask this topic