Data Structures & AlgorithmsVery hardGATE CS · competitive programming
What is the worst-case time complexity of finding the minimum in a rotated sorted array that may contain duplicates, using a binary search that compares a[mid] with a[hi]?
- A. O(log n)
- B. O(n log n)
- C. O(1)
- D. O(n)
Correct answer: D. O(n)
When a[lo] = a[mid] = a[hi], the minimum could lie on either side, so the search can only shrink the range by one index. With all values equal this takes n steps.
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
- For binary search over 1000 distinct sorted values with mid = (lo + hi) / 2 and the loop running while lo ≤ hi, what is the largest number…
- In a Fenwick tree with n = 16, an update at index 5 moves i ← i + (i & −i) while i ≤ n. How many tree nodes does the update touch?
- Brian Kernighan's loop (x &= x − 1 until x is 0) counts the set bits of x. What is the total number of loop iterations when it is run once…
- How many integers x in [0, 1023] have no two adjacent 1 bits in their 10-bit binary form?
- In [1, 2, 1, 3, 2, 5] exactly two values appear once and every other value appears twice. Which two values are they?
- A 10×20 array A[1..10][1..20] is stored column-major. The base address is 1000 and each element takes 8 bytes. What is the address of…