Data Structures & AlgorithmsHardGATE CS · DSA

Every row and every column of this 4 × 4 matrix is sorted ascending (rows separated by ';'): 9 11 16 36; 14 19 37 42; 29 40 45 50; 41 46 52 59. The staircase search starts at the top-right corner, moves left if the element is larger than the key and down if smaller. How many elements does it examine when searching for 16, and is the key found?

  1. A. 3 elements; found
  2. B. 2 elements; not found
  3. C. 1 elements; found
  4. D. 2 elements; found

Correct answer: D. 2 elements; found

Each comparison discards a row or a column, so at most R + C − 1 = 7 elements are examined. Tracing for 16 examines 2 element(s) and finds it.

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

Exams that ask this topic