0
0
DSA C++programming~10 mins

Binary Search Recursive Approach 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 calculate the middle index in the binary search.

DSA C++
int mid = low + ([1] - low) / 2;
Drag options to blanks, or click blank then click option'
Amid
Bhigh
Clow
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using low twice instead of high and low.
Using size instead of high.
2fill in blank
medium

Complete the code to return the index when the target is found.

DSA C++
if (arr[mid] == target) return [1];
Drag options to blanks, or click blank then click option'
Alow
B-1
Cmid
Dhigh
Attempts:
3 left
💡 Hint
Common Mistakes
Returning low or high instead of mid.
Returning -1 which means not found.
3fill in blank
hard

Fix the error in the recursive call for searching the left half.

DSA C++
return binarySearch(arr, low, [1] - 1, target);
Drag options to blanks, or click blank then click option'
Atarget
Blow
Chigh
Dmid
Attempts:
3 left
💡 Hint
Common Mistakes
Using low or high instead of mid.
Passing target as index.
4fill in blank
hard

Fill both blanks to complete the recursive call for searching the right half.

DSA C++
return binarySearch(arr, [1] + 1, [2], target);
Drag options to blanks, or click blank then click option'
Amid
Blow
Chigh
Dtarget
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping low and high.
Using target as index.
5fill in blank
hard

Fill all three blanks to complete the base case and recursive calls of binary search.

DSA C++
if (low > high) return [1];
int mid = low + (high - low) / 2;
if (arr[mid] == target) return [2];
else if (arr[mid] > target) return binarySearch(arr, low, [3] - 1, target);
else return binarySearch(arr, mid + 1, high, target);
Drag options to blanks, or click blank then click option'
A-1
Bmid
Dlow
Attempts:
3 left
💡 Hint
Common Mistakes
Returning wrong values in base case or recursive calls.
Using low instead of mid in recursive calls.