Data Structures & AlgorithmsModerateGATE CS · TCS NQT
A sorted array of distinct values was rotated some number of times. Which technique finds the minimum element in O(log n) time, and why does it work?
- A. Linear scan with two pointers, since the sorted order allows skipping half the array each time
- B. Hashing every value, since a hash table gives the minimum in constant time
- C. Binary search: compare the middle element with the last element to decide which half contains the rotation point
- D. Sorting the array again, since sorting is cheaper after a rotation
Correct answer: C. Binary search: compare the middle element with the last element to decide which half contains the rotation point
If a[mid] > a[hi], the rotation point (the minimum) lies right of mid; otherwise it lies at or left of mid. Each comparison discards half the range, so the search is O(log n).
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
- In one operation you may increase any single element by 1. What is the minimum total number of operations needed to make the array [1, 1…
- Choose two NON-OVERLAPPING non-empty contiguous subarrays of [4, -1, 2, 6, -3, 5, -2] (the first must end before the second starts) and…
- In how many ways can a 2 × 9 board be tiled exactly with 2 × 1 dominoes (placed vertically) and 2 × 2 squares, with no overlaps and no…
- How many integers x with 1 ≤ x ≤ 1000 are perfect squares or perfect cubes (or both)?
- Intervals (inclusive integer ranges) are [(1, 3), (2, 5), (4, 9), (6, 7), (8, 12), (9, 10)]. What is the minimum number of intervals whose…
- A binary string is "10110010". Flipping position i costs weight[i] = [3, 1, 4, 1, 5, 9, 2, 6]. You want the string to alternate (0101…