0
0
DSA C++programming~10 mins

Why Binary Search and What Sorted Order Gives You in DSA C++ - Test Your Knowledge

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

Complete the code to start binary search with the middle index.

DSA C++
int mid = (low [1] high) / 2;
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition
Using multiplication instead of addition
2fill in blank
medium

Complete the code to compare the middle element with the target value.

DSA C++
if (arr[mid] [1] target) {
Drag options to blanks, or click blank then click option'
A<=
B>=
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using inequality instead of equality
Using less than or greater than operators here
3fill in blank
hard

Fix the error in updating the low index when target is greater than middle element.

DSA C++
if (arr[mid] < target) {
    low = [1];
}
Drag options to blanks, or click blank then click option'
Amid + 1
Bmid - 1
Clow + 1
Dhigh - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Setting low to mid - 1
Setting low to high - 1
4fill in blank
hard

Fill both blanks to correctly update high index and return found index.

DSA C++
if (arr[mid] > target) {
    high = [1];
} else {
    return [2];
}
Drag options to blanks, or click blank then click option'
Amid - 1
Bmid + 1
Cmid
Dlow
Attempts:
3 left
💡 Hint
Common Mistakes
Setting high to mid + 1
Returning low instead of mid
5fill in blank
hard

Fill all three blanks to complete the binary search loop and return -1 if not found.

DSA C++
while ([1] <= [2]) {
    int mid = (low + high) / 2;
    if (arr[mid] == target) {
        return [3];
    }
    // other conditions omitted
}
return -1;
Drag options to blanks, or click blank then click option'
Alow
Bhigh
Cmid
Dtarget
Attempts:
3 left
💡 Hint
Common Mistakes
Using target in loop condition
Returning low or high instead of mid