0
0
DSA C++programming~10 mins

Find Peak Element Using Binary Search 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 middle index for binary search.

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

Complete the condition to check if mid is a peak element.

DSA C++
if ((mid == 0 || nums[mid] > nums[mid - 1]) && (mid == n - 1 || nums[mid] > nums[[1]]))
Drag options to blanks, or click blank then click option'
Amid + 1
Bn - 1
Cmid
Dmid - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using mid - 1 instead of mid + 1.
Comparing with mid instead of mid + 1.
3fill in blank
hard

Fix the error in updating the high pointer when the left neighbor is greater.

DSA C++
if (nums[mid] < nums[[1]]) {
    high = mid [2] 1;
}
Drag options to blanks, or click blank then click option'
Amid - 1
Bmid + 1
C-
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using mid + 1 instead of mid - 1.
Using addition instead of subtraction.
4fill in blank
hard

Fill both blanks to update low pointer when right neighbor is greater and to return the peak index.

DSA C++
else {
    low = mid [1] 1;
}

return [2];
Drag options to blanks, or click blank then click option'
A+
B-
Cmid
Dlow
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition for low.
Returning low instead of mid.
5fill in blank
hard

Fill all three blanks to complete the binary search loop and return the peak element index.

DSA C++
while (low [1] high) {
    int mid = low + (high - low) / 2;
    if ((mid == 0 || nums[mid] > nums[mid - 1]) && (mid == n - 1 || nums[mid] > nums[mid + 1])) {
        return [2];
    } else if (mid > 0 && nums[mid] < nums[mid - 1]) {
        high = mid [3] 1;
    } else {
        low = mid + 1;
    }
}
return low;
Drag options to blanks, or click blank then click option'
A<=
Bmid
C-
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of <= in the loop condition.
Returning low instead of mid inside the loop.
Using addition instead of subtraction when updating high.