0
0
DSA Typescriptprogramming

Find Peak Element Using Binary Search in DSA Typescript

Choose your learning style9 modes available
Mental Model
A peak element is one that is bigger than its neighbors. We use binary search to quickly find such a peak by checking the middle and deciding which side to search next.
Analogy: Imagine walking on hills and valleys. If you stand somewhere and see the slope going up on one side, you know a peak must be in that direction. You keep moving halfway towards that side until you find the top.
Array: [1, 3, 2, 5, 4]
Indexes:  0  1  2  3  4

Initial search range:
start -> 0
end   -> 4

Middle points to index 2 (value 2)
Neighbors: 3 (left), 5 (right)
Dry Run Walkthrough
Input: array: [1, 3, 2, 5, 4]
Goal: Find any peak element index where element is greater than neighbors
Step 1: Calculate mid = (0 + 4) // 2 = 2; compare nums[2] = 2 with nums[3] = 5
[1, 3, 2, 5, 4]
start=0, end=4, mid=2
Why: We check if right neighbor is bigger to decide search direction
Step 2: Since nums[2] < nums[3], move start to mid + 1 = 3
[1, 3, 2, 5, 4]
start=3, end=4
Why: Peak must be on right side because slope goes up
Step 3: Calculate mid = (3 + 4) // 2 = 3; compare nums[3] = 5 with nums[4] = 4
[1, 3, 2, 5, 4]
start=3, end=4, mid=3
Why: Check if mid is peak by comparing with right neighbor
Step 4: Since nums[3] > nums[4], move end to mid = 3
[1, 3, 2, 5, 4]
start=3, end=3
Why: Peak is at mid or left side because slope goes down on right
Result:
Peak found at index 3 with value 5
Annotated Code
DSA Typescript
class Solution {
  findPeakElement(nums: number[]): number {
    let start = 0
    let end = nums.length - 1

    while (start < end) {
      const mid = Math.floor((start + end) / 2)
      // Compare middle element with its right neighbor
      if (nums[mid] < nums[mid + 1]) {
        // Peak must be on right side
        start = mid + 1
      } else {
        // Peak is at mid or on left side
        end = mid
      }
    }
    // start and end converge to peak index
    return start
  }
}

// Driver code
const solution = new Solution()
const nums = [1, 3, 2, 5, 4]
const peakIndex = solution.findPeakElement(nums)
console.log(`Peak found at index ${peakIndex} with value ${nums[peakIndex]}`)
while (start < end) {
loop until search range narrows to one element
const mid = Math.floor((start + end) / 2)
calculate middle index to check
if (nums[mid] < nums[mid + 1]) {
compare mid element with right neighbor to decide direction
start = mid + 1
move start to right half because slope goes up
end = mid
move end to mid because peak is at mid or left side
return start
start and end converge to peak index
OutputSuccess
Peak found at index 3 with value 5
Complexity Analysis
Time: O(log n) because binary search halves the search space each step
Space: O(1) because only a few variables are used regardless of input size
vs Alternative: Compared to linear scan O(n), binary search is much faster for large arrays
Edge Cases
Array with single element
Returns index 0 as peak since it has no neighbors
DSA Typescript
while (start < end) {
Strictly increasing array like [1, 2, 3, 4]
Peak is last element; binary search moves right until end
DSA Typescript
if (nums[mid] < nums[mid + 1]) {
Strictly decreasing array like [4, 3, 2, 1]
Peak is first element; binary search moves left until start
DSA Typescript
else { end = mid }
When to Use This Pattern
When you need to find a local maximum in an array quickly, use binary search on neighbors to reduce search space efficiently.
Common Mistakes
Mistake: Comparing mid element with left neighbor instead of right neighbor
Fix: Always compare nums[mid] with nums[mid + 1] to decide direction
Mistake: Using start <= end in loop causing infinite loop
Fix: Use start < end to ensure loop ends when pointers converge
Summary
Finds an index of a peak element where neighbors are smaller.
Use when you want to find a local maximum faster than scanning all elements.
The key insight is comparing middle element with its right neighbor to decide which half contains a peak.