Data Structures & AlgorithmsVery hardCodeforces Div2 B-D · ICPC regional

An array has 10^5 values and you need the k-th smallest value among all pair sums a[i] + a[j] with i < j. Which approach is the right fit?

  1. A. Binary search on the answer with a two-pointer count of pairs whose sum is at most the candidate, after sorting
  2. B. Sort all pair sums explicitly, since n^2 pairs fit in memory
  3. C. Dijkstra over index pairs, treating each pair as a graph node
  4. D. Segment tree over the array values, updating one pair per query

Correct answer: A. Binary search on the answer with a two-pointer count of pairs whose sum is at most the candidate, after sorting

Sorting the array lets a two-pointer scan count pairs with sum at most x in O(n), so binary search over x finds the k-th sum in O(n log(range)). Materialising n^2 sums is too large.

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

Exams that ask this topic