Challenge - 5 Problems
Peak Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind the peak element index in the array
What is the output index of the peak element for the given array using binary search?
DSA Java
public class PeakElement { public static int findPeakElement(int[] nums) { int left = 0, right = nums.length - 1; while (left < right) { int mid = left + (right - left) / 2; if (nums[mid] > nums[mid + 1]) { right = mid; } else { left = mid + 1; } } return left; } public static void main(String[] args) { int[] nums = {1, 3, 20, 4, 1, 0}; System.out.println(findPeakElement(nums)); } }
Attempts:
2 left
💡 Hint
Check where the array value is greater than its next element to find the peak.
✗ Incorrect
The binary search compares mid element with mid+1 element. If mid is greater, the peak lies on the left side including mid. Otherwise, it lies on the right side. The loop ends when left == right, which is the peak index.
❓ Predict Output
intermediateOutput of peak element index for a strictly increasing array
What index will the binary search return as the peak element for this strictly increasing array?
DSA Java
public class PeakElement { public static int findPeakElement(int[] nums) { int left = 0, right = nums.length - 1; while (left < right) { int mid = left + (right - left) / 2; if (nums[mid] > nums[mid + 1]) { right = mid; } else { left = mid + 1; } } return left; } public static void main(String[] args) { int[] nums = {1, 2, 3, 4, 5, 6}; System.out.println(findPeakElement(nums)); } }
Attempts:
2 left
💡 Hint
In a strictly increasing array, the last element is the peak.
✗ Incorrect
Since the array is strictly increasing, each element is less than the next. The binary search moves right until it reaches the last index, which is the peak.
🔧 Debug
advancedIdentify the error in the binary search peak element code
What error will this code produce when run?
DSA Java
public class PeakElement { public static int findPeakElement(int[] nums) { int left = 0, right = nums.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] > nums[mid + 1]) { right = mid - 1; } else { left = mid + 1; } } return left; } public static void main(String[] args) { int[] nums = {1, 3}; System.out.println(findPeakElement(nums)); } }
Attempts:
2 left
💡 Hint
Check the condition and indexing inside the loop carefully.
✗ Incorrect
The code accesses nums[mid + 1] without ensuring mid+1 is within bounds. When mid is the last index, this causes ArrayIndexOutOfBoundsException.
🧠 Conceptual
advancedWhy does binary search work for finding a peak element?
Which statement best explains why binary search can find a peak element in an unsorted array?
Attempts:
2 left
💡 Hint
Think about how the comparison between mid and mid+1 guides the search.
✗ Incorrect
The array may not be sorted, but a peak element always exists. Comparing nums[mid] and nums[mid+1] tells which side has a peak, allowing binary search to discard half the array each time.
❓ Predict Output
expertFind peak element index in array with multiple peaks
What is the output index of the peak element for the given array using the binary search method?
DSA Java
public class PeakElement { public static int findPeakElement(int[] nums) { int left = 0, right = nums.length - 1; while (left < right) { int mid = left + (right - left) / 2; if (nums[mid] > nums[mid + 1]) { right = mid; } else { left = mid + 1; } } return left; } public static void main(String[] args) { int[] nums = {1, 3, 2, 5, 4, 6, 1}; System.out.println(findPeakElement(nums)); } }
Attempts:
2 left
💡 Hint
The binary search returns one peak, not necessarily the highest peak.
✗ Incorrect
The binary search finds a peak by comparing mid and mid+1. It will return index 1 where value 3 is a peak (greater than neighbors 1 and 2).
