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?
- A. 7
- B. 4
- C. 5
- 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).
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- The numbers [1, 6, 3, 8, 4] can be arranged in any order in a row. Maximise the sum of absolute differences between neighbouring numbers…
- Using only swaps of ADJACENT elements, what is the minimum number of swaps needed to turn [1, 2, 3, 2, 1, 3, 3] into a palindrome? (It is…
- How many contiguous subarrays of [3, 1, 3, 2, 5, 2, 5] contain exactly one occurrence of their maximum value?
- What is the length of the longest contiguous block of [1, 2, 3, 1, 2, 3, 4, 2, 3, 4] that appears at least twice in the array WITHOUT…
- What is the length of the longest subsequence of [1, 2, 4, 5, 7, 8, 10] that is strictly increasing and in which every two consecutive…
- Using swaps of ADJACENT elements only, what is the minimum number of swaps needed to sort the binary array [1, 0, 1, 1, 0, 0, 1, 0] so…