Data Structures & AlgorithmsVery hardCodeforces Div2 C · GATE CS
A binary search finds the largest x with xx <= 50 using the bounds 0 and 50.
A binary search finds the largest x with x*x <= 50 using the bounds 0 and 50.
lo, hi = 0, 50
while lo < hi:
mid = (lo + hi + 1) // 2
if mid * mid <= 50: lo = mid
else: hi = mid - 1
print(lo)What is printed?- A. 8
- B. 25
- C. 7
- D. 6
Correct answer: C. 7
Rounding mid up, (lo + hi + 1) // 2, is needed when the update is lo = mid: with rounding down, lo = mid can stall forever when lo and hi are adjacent. The variant here keeps the same bounds but moves the boundary by one, so it reports the first failing value (8) instead of 7.
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 routine computes the number of subarrays whose sum is divisible by 5, using the remainder of each prefix sum. python a = [3, -1, 4, -6…
- A problem asks for the minimum number of refuelling stops on a road, where the car has a fuel capacity and each station offers some fuel…
- For the problem 'choose the largest set of non-overlapping intervals', which greedy rule is correct?
- A DP over prefixes of an array computes the number of ways to split it into segments where each segment's sum is at most L. Which…
- Which problem is the standard application of 'binary search on the answer' where the feasibility test itself is a greedy scan?
- In bitmask DP over subsets of n items, what is the typical total time for the TSP-style recurrence dp[mask][last] with transitions to…