Data Structures & AlgorithmsVery hardCodeforces Div2 D · ICPC regional

Each row and each column of a 4 × 4 matrix is sorted in increasing order: [[1, 5, 9, 12], [2, 6, 10, 15], [3, 7, 11, 16], [4, 8, 13, 20]]. What is the 7th smallest value in the whole matrix?

  1. A. 10
  2. B. 8
  3. C. 7
  4. D. 20

Correct answer: C. 7

Binary search on a value x, counting entries ≤ x with a staircase walk from the bottom-left corner: the count first reaches 7 at x = 7 (1, 2, 3, 4, 5, 6, 7). Reading row by row gives 10, the 8th smallest is 8, and 20 is the largest entry.

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

Exams that ask this topic