Data Structures & AlgorithmsHardTCS NQT · Codeforces Div2 B

Closed integer intervals are [(1, 4), (2, 3), (5, 7), (4, 6), (8, 9), (7, 8)]. How many unordered pairs of intervals share at least one common integer point?

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

Correct answer: C. 5

Two closed intervals intersect exactly when the larger of the starts is at most the smaller of the ends. Checking all pairs is O(n²); sorting by start with a sweep gives O(n log n). Counting touching intervals as non-overlapping drops pairs such as (7, 8) and (8, 9).

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

Exams that ask this topic