0
0
DSA Javascriptprogramming

Find Peak Element Using Binary Search in DSA Javascript

Choose your learning style9 modes available
Mental Model
A peak element is one that is bigger than its neighbors. We can find it quickly by checking the middle and deciding which side to search next.
Analogy: Imagine hiking on hills. If you stand somewhere and see the hill going up on one side, you walk that way to find the top. If it goes down, you walk the other way.
Index: 0   1   2   3   4   5   6
Value: 1 -> 3 -> 5 -> 4 -> 2 -> 6 -> 1
               ↑ mid pointer
Dry Run Walkthrough
Input: array: [1, 3, 5, 4, 2, 6, 1]
Goal: Find any peak element index where the element is greater than neighbors
Step 1: Calculate mid = (0 + 6) // 2 = 3; check element at index 3 (value 4)
Array: 1 -> 3 -> 5 -> [4↑mid] -> 2 -> 6 -> 1
Why: We start by checking the middle element to decide which half to search next
Step 2: Compare mid element 4 with next element 2; since 4 > 2, move search to left half
Search range: indices 0 to 3
Array: 1 -> 3 -> 5 -> [4↑mid]
Why: If mid is greater than next, peak must be on left side or at mid
Step 3: Calculate new mid = (0 + 3) // 2 = 1; check element at index 1 (value 3)
Array: 1 -> [3↑mid] -> 5 -> 4
Why: We narrow search to left half and check its middle
Step 4: Compare mid element 3 with next element 5; since 3 < 5, move search to right half
Search range: indices 2 to 3
Array: 5 -> [4]
Why: If mid is less than next, peak must be on right side
Step 5: Calculate new mid = (2 + 3) // 2 = 2; check element at index 2 (value 5)
Array: [5↑mid] -> 4
Why: Check middle of new range to find peak
Step 6: Compare mid element 5 with next element 4; since 5 > 4, peak found at index 2
Peak element: 5 at index 2
Why: Element 5 is greater than neighbors, so it is a peak
Result:
Peak element found at index 2 with value 5
Annotated Code
DSA Javascript
class Solution {
  findPeakElement(nums) {
    let left = 0;
    let right = nums.length - 1;
    while (left < right) {
      const mid = Math.floor((left + right) / 2);
      if (nums[mid] < nums[mid + 1]) {
        left = mid + 1; // move right because peak is there
      } else {
        right = mid; // move left including mid because peak is here or left
      }
    }
    return left; // left == right is peak index
  }
}

// Driver code
const solution = new Solution();
const nums = [1, 3, 5, 4, 2, 6, 1];
const peakIndex = solution.findPeakElement(nums);
console.log(`Peak element found at index ${peakIndex} with value ${nums[peakIndex]}`);
while (left < right) {
loop until search range narrows to one element
const mid = Math.floor((left + right) / 2);
find middle index of current search range
if (nums[mid] < nums[mid + 1]) {
compare mid element with next to decide search direction
left = mid + 1; // move right because peak is there
if next is bigger, peak must be on right side
right = mid; // move left including mid because peak is here or left
else peak is on left side or at mid
return left; // left == right is peak index
when left meets right, peak found
OutputSuccess
Peak element found at index 2 with value 5
Complexity Analysis
Time: O(log n) because we halve the search range each step
Space: O(1) because we use only a few variables, no extra space
vs Alternative: Linear scan takes O(n) by checking every element; binary search is faster with O(log n)
Edge Cases
Array with one element
Returns index 0 as peak since it's the only element
DSA Javascript
while (left < right) {
Strictly increasing array like [1,2,3,4]
Peak is last element because each next is bigger; binary search moves right until end
DSA Javascript
if (nums[mid] < nums[mid + 1]) {
Strictly decreasing array like [4,3,2,1]
Peak is first element; binary search moves left narrowing to start
DSA Javascript
else { right = mid; }
When to Use This Pattern
When you need to find a local maximum in an array quickly, use binary search on neighbors to jump towards a peak.
Common Mistakes
Mistake: Checking only mid element without comparing neighbors
Fix: Always compare mid with mid+1 to decide which side to search
Mistake: Using mid-1 without checking array bounds
Fix: Use mid+1 safely since mid < right, avoid out-of-bound errors
Summary
Finds an index of a peak element where neighbors are smaller.
Use when you want to find a local maximum efficiently in an unsorted array.
Compare middle element with next to decide which half contains a peak.