Data Structures & AlgorithmsModerateGATE CS · competitive programming
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 of iterations needed over all present targets?
- A. 10
- B. 9
- C. 11
- D. 500
Correct answer: A. 10
Each probe halves the range, and 2⁹ = 512 < 1000 ≤ 2¹⁰ = 1024. So at most 10 probes are needed.
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 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…
- An n×n matrix is rotated 90° clockwise. The original is [[1,2,3],[4,5,6],[7,8,9]]. Which value is now in the top-left corner?