Data Structures & AlgorithmsVery hardGATE CS · TCS NQT
A binary search on the sorted array 1, 2, ..., 100 looks for 37 using mid = (lo + hi) // 2 with lo = 0 and hi = 99. How many comparisons with array elements are made, counting the comparison that finds the target?
- A. 4
- B. 5
- C. 7
- D. 3
Correct answer: D. 3
The mids are 49, 24 and 36 (values 50, 25 and 37), so the target is found on the third comparison.
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
- How many unordered index pairs i < j in the sorted array [1, 3, 4, 6, 8, 9, 11] satisfy a[i] + a[j] = 12?
- How many distinct non-empty substrings does the string aaaa have?
- An array has only positive integers and you need the longest contiguous subarray whose sum is at most K, with n up to 10^6. Which method…
- How many contiguous subarrays of [3, 1, 4, 1, 5, 9] have a prime sum?
- What is the minimum number of elements to delete from [4, 7, 2, 9, 5] so that the remaining sum is divisible by 3? (The empty remainder is…
- How many index pairs i < j of [1, 2, 4, 6, 7, 9] have a sum strictly between 5 and 10?