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?
- A. 7 elements; not found
- B. 7 elements; found
- C. 6 elements; not found
- 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.
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
- Sort [−1, 3, 2, 2, 6, 1, −2] and, for each i, move two pointers to find the triplet sum closest to 5. What is that closest sum?
- What is the maximum product of a non-empty contiguous subarray of [3, 4, 3, 3, −2, −2, −1, 3]? (Track both the maximum and the minimum…
- Bubble sort (ascending; each pass compares adjacent pairs left to right and swaps if out of order) is applied to [3, 37, 10, 52, 30, 23…
- Records are sorted with a composite key. What is printed? python recs = [('anu', 80), ('zoya', 70), ('dev', 70), ('ravi', 70), ('meera'…
- Two sorted arrays of sizes m and n (m ≤ n) are given. What is the best possible time to find the median of their union?
- A circular queue in an array of size n uses front and rear indices and keeps one slot always empty. What is the maximum number of elements…