Data Structures & AlgorithmsHardGATE CS · Product placement

Every row and every column of this 4 × 4 matrix is sorted ascending (rows separated by ';'): 3 8 19 33; 17 22 35 42; 24 36 49 52; 38 50 53 58. 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 47, and is the key found?

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

Correct answer: A. 7 elements; not found

Each comparison discards a row or a column, so at most R + C − 1 = 7 elements are examined. Tracing for 47 examines 7 element(s) and walks off the matrix without finding it.

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

Exams that ask this topic