0
0
DSA C++programming~10 mins

Binary Search on Answer Technique in DSA C++ - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the search range for binary search on answer.

DSA C++
int low = 0;
int high = [1];
Drag options to blanks, or click blank then click option'
A100
B1000
C10
D1e9
Attempts:
3 left
💡 Hint
Common Mistakes
Setting high too low and missing the correct answer.
Using a small fixed number like 10 or 100 without reasoning.
2fill in blank
medium

Complete the code to calculate the middle value in binary search on answer.

DSA C++
int mid = low + ([1] - low) / 2;
Drag options to blanks, or click blank then click option'
Ahigh
Blow
Cmid
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using (low + high) / 2 directly which can overflow.
Using low instead of high in the calculation.
3fill in blank
hard

Fix the error in the condition to update the search range based on feasibility check.

DSA C++
if (isPossible([1])) {
    high = mid - 1;
} else {
    low = mid + 1;
}
Drag options to blanks, or click blank then click option'
Amid
Blow
Chigh
Dmid + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Passing low or high instead of mid to isPossible.
Using mid + 1 which is not the current candidate.
4fill in blank
hard

Fill both blanks to complete the binary search loop condition and update low when answer is not possible.

DSA C++
while ([1] <= [2]) {
    int mid = low + (high - low) / 2;
    if (isPossible(mid)) {
        high = mid - 1;
    } else {
        low = mid + 1;
    }
}
Drag options to blanks, or click blank then click option'
Alow
Bhigh
Cmid
Dlow + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using mid in the while condition.
Using low + 1 in the while condition instead of high.
5fill in blank
hard

Fill all three blanks to return the final answer after binary search on answer completes.

DSA C++
int binarySearchAnswer() {
    int low = 0, high = 1e9;
    int result = -1;
    while (low <= high) {
        int mid = low + (high - low) / 2;
        if (isPossible(mid)) {
            result = [1];
            high = [2];
        } else {
            low = [3];
        }
    }
    return result;
}
Drag options to blanks, or click blank then click option'
Amid
Bmid - 1
Cmid + 1
Dhigh
Attempts:
3 left
💡 Hint
Common Mistakes
Not updating result when mid is possible.
Incorrectly updating high or low boundaries.