Data Structures & AlgorithmsHardGATE CS · Product placement
A sorted array was rotated to give [58, 67, 70, 71, 77, 3, 12, 14, 55]. Binary search finds the index of the minimum: while lo < hi, mid = (lo + hi) // 2; if a[mid] > a[hi] then lo = mid + 1 else hi = mid. What index is returned and how many iterations run?
- A. index 5, 3 iterations
- B. index 4, 3 iterations
- C. index 6, 3 iterations
- D. index 5, 4 iterations
Correct answer: A. index 5, 3 iterations
Comparing with a[hi] tells which half is sorted; the minimum 3 is at index 5 (the rotation point), reached after 3 iterations.
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
- A string s has length n. What is the time complexity of building s by repeated concatenation s = s + c in a language with immutable…
- What does this loop-counting code print? (Python) python n, c = 10, 0 for i in range(n): for j in range(i + 1, n): for k in range(j + 1…
- Over all left rotations of [5, 1, 5, 3, 9, 4], what is the maximum value of Σ i·a[i] (0-based)? (Use R(j) = R(j − 1) + sum − n·a[n − j] to…
- Items K, L, M, N, O, P are pushed onto an empty stack in this order, and a pop may happen at any time. Which pop order is NOT possible?
- Keys 121, 29, 37, 91, 13, 33, 39 are inserted in this order into an empty table of size 13 by double hashing: h1(k) = k mod 13, h2(k) = 7…
- Delete every alternate node (2nd, 4th, …) of a linked list, then reverse what remains. What is printed? python def delalt(h): p = h while…