0
0
DSA Goprogramming

Find Peak Element Using Binary Search in DSA Go

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 an element by checking the middle and deciding which half to search next.
Analogy: Imagine climbing a mountain range where you want to find a peak. Instead of walking the whole range, you look at the middle point and decide if you should go left or right based on which side is higher, because a peak must exist in that direction.
Array: [1, 3, 2, 4, 1]
Indexes:  0  1  2  3  4

Initial view:
start -> 0, end -> 4
mid -> 2 (value 2)
Neighbors: left=3, right=4
Dry Run Walkthrough
Input: array: [1, 3, 2, 4, 1]
Goal: Find any peak element index where element is greater than neighbors
Step 1: Calculate mid = (0 + 4) / 2 = 2; compare nums[mid] = 2 with nums[mid+1] = 4
[1, 3, 2, 4, 1]
start=0, end=4, mid=2
nums[mid]=2, nums[mid+1]=4
Why: If right neighbor is bigger, peak must be on right side
Step 2: Move start to mid + 1 = 3 to search right half
[1, 3, 2, 4, 1]
start=3, end=4
Why: We discard left half because peak must be on right side
Step 3: Calculate mid = (3 + 4) / 2 = 3; compare nums[mid] = 4 with nums[mid+1] = 1
[1, 3, 2, 4, 1]
start=3, end=4, mid=3
nums[mid]=4, nums[mid+1]=1
Why: nums[mid] > nums[mid+1], so peak could be at mid or left side
Step 4: Move end to mid = 3 to narrow search
[1, 3, 2, 4, 1]
start=3, end=3
Why: We narrow down to mid because it might be the peak
Result:
Peak found at index 3 with value 4
Annotated Code
DSA Go
package main

import "fmt"

func findPeakElement(nums []int) int {
    start, end := 0, len(nums)-1
    for start < end {
        mid := (start + end) / 2
        if nums[mid] < nums[mid+1] {
            start = mid + 1
        } else {
            end = mid
        }
    }
    return start
}

func main() {
    nums := []int{1, 3, 2, 4, 1}
    peakIndex := findPeakElement(nums)
    fmt.Printf("Peak found at index %d with value %d\n", peakIndex, nums[peakIndex])
}
mid := (start + end) / 2
Calculate middle index to check
if nums[mid] < nums[mid+1] {
Compare mid element with right neighbor to decide search direction
start = mid + 1
Move start to right half if right neighbor is bigger
end = mid
Move end to mid if mid is greater or equal to right neighbor
return start
Return the index where start meets end, the peak element
OutputSuccess
Peak found at index 3 with value 4
Complexity Analysis
Time: O(log n) because we halve the search space each step using binary search
Space: O(1) because we use only a few variables, no extra data structures
vs Alternative: Compared to linear scan O(n), binary search is faster for large arrays by reducing steps exponentially
Edge Cases
Single element array
Returns index 0 as the peak since it's the only element
DSA Go
start, end := 0, len(nums)-1
Strictly increasing array like [1,2,3,4]
Returns last index as peak because last element is greater than previous
DSA Go
if nums[mid] < nums[mid+1] { start = mid + 1 }
Strictly decreasing array like [4,3,2,1]
Returns first index as peak because first element is greater than next
DSA Go
else { end = mid }
When to Use This Pattern
When you need to find a local maximum in an array efficiently, use binary search on neighbors to reduce search space quickly.
Common Mistakes
Mistake: Checking only nums[mid] without comparing neighbors
Fix: Always compare nums[mid] with nums[mid+1] to decide which half contains a peak
Mistake: Using mid = (start + end + 1) / 2 causing infinite loop
Fix: Use mid = (start + end) / 2 and adjust start/end carefully to avoid infinite loops
Summary
Finds an index of a peak element where neighbors are smaller
Use when you want to find a local maximum quickly without scanning all elements
The key insight is comparing mid element with its right neighbor to decide which half contains a peak