ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Find Peak Element in Array

Go

Goal -- WPM

Ready
Exercise Algorithm Area
1package main
2
3import "fmt"
4
5// findPeakElement finds a peak element in an array.
6// A peak element is an element that is strictly greater than its neighbors.
7// The array is assumed to have at least one peak.
8func findPeakElement(nums []int) int {
9 left := 0
10 right := len(nums) - 1
11
12 // Loop invariant: a peak element exists within the range [left, right].
13 // The search space is halved in each iteration.
14 for left < right {
15 mid := left + (right-left)/2
16
17 // If the middle element is less than the element to its right,
18 // it means a peak must exist in the right half (including mid+1).
19 if nums[mid] < nums[mid+1] {
20 left = mid + 1
21 } else {
22 // Otherwise, a peak must exist in the left half (including mid).
23 // This is because nums[mid] >= nums[mid+1]. If nums[mid] is also
24 // greater than nums[mid-1] (or mid is 0), then mid is a peak.
25 // If nums[mid] < nums[mid-1], then a peak exists to the left.
26 right = mid
27 }
28 }
29
30 // When left == right, we have found a peak element.
31 return left
32}
33
34func main() {
35 nums1 := []int{1, 2, 3, 1}
36 fmt.Printf("Peak element index in %v: %d\n", nums1, findPeakElement(nums1))
37
38 nums2 := []int{1, 2, 1, 3, 5, 6, 4}
39 fmt.Printf("Peak element index in %v: %d\n", nums2, findPeakElement(nums2))
40
41 nums3 := []int{1}
42 fmt.Printf("Peak element index in %v: %d\n", nums3, findPeakElement(nums3))
43
44 nums4 := []int{3, 2, 1}
45 fmt.Printf("Peak element index in %v: %d\n", nums4, findPeakElement(nums4))
46}
Algorithm description viewbox

Find Peak Element in Array

Algorithm description:

This Go program implements a binary search algorithm to efficiently find a peak element in an array. A peak element is defined as an element that is strictly greater than its adjacent neighbors. The problem assumes that the array has at least one peak and that elements at indices -1 and n (where n is the array length) are negative infinity. This approach is useful in scenarios where you need to quickly locate a local maximum in a dataset, such as in signal processing or optimization problems.

Algorithm explanation:

The `findPeakElement` function utilizes a binary search approach to locate a peak element in `O(log n)` time complexity, where `n` is the number of elements in the array. The space complexity is `O(1)` as it only uses a few variables. The core idea relies on the invariant that a peak element always exists within the current search range `[left, right]`. In each step, we examine the middle element `mid`. If `nums[mid] < nums[mid+1]`, it implies that the peak must lie to the right of `mid` because the array is increasing at `mid`. Conversely, if `nums[mid] >= nums[mid+1]`, the peak must lie at or to the left of `mid`. This is because if `nums[mid]` is not a peak, then `nums[mid-1]` must be greater, indicating a peak to the left. The loop terminates when `left == right`, at which point `left` (or `right`) is guaranteed to be the index of a peak element. Edge cases like a single-element array are handled implicitly by the loop condition and the assumption of at least one peak.

Pseudocode:

function findPeakElement(nums):
  left = 0
  right = length(nums) - 1

  while left < right:
    mid = left + (right - left) / 2
    if nums[mid] < nums[mid + 1]:
      left = mid + 1
    else:
      right = mid

  return left