Find Peak Element [Medium]

22 Aug 2026, Updated: 23 Sep 2026 2 min read
3
Find Peak Element requires finding an element that is greater than its neighboring elements.

Problem

Given an integer array nums, find a peak element and return its index. An element is a peak if it is strictly greater than its neighbors.

You may assume that nums[-1] = nums[n] = -∞. The array may contain multiple peaks, so returning the index of any peak is valid.

Example(s)

Example 1

Input
nums = [1,2,3,1]
Output
2

Example 2

Input
nums = [1,2,1,3,5,6,4]
Output
5

Solution

This problem uses Binary Search. Compare the middle element with the next element. If nums[mid] > nums[mid + 1], a peak exists on the left side including mid. Otherwise, a peak must exist on the right side.

Continue reducing the search range until both boundaries meet. The remaining index is a peak.
class Solution {
    public int findPeakElement(int[] nums) {
        return binarySearch(0, nums.length - 1, nums);
    }

    private int binarySearch(int i, int j, int[] nums) {
        if (i >= j) {
            return i;
        }

        int mid = i + (j - i) / 2;

        if (nums[mid] > nums[mid + 1]) {
            return binarySearch(i, mid, nums);
        } else {
            return binarySearch(mid + 1, j, nums);
        }
    }
}

Complexity

The time complexity is O(log n) because the search range is divided approximately in half at every step. The space complexity is O(log n) due to the recursive call stack.
Nagesh Chauhan

Nagesh Chauhan

Principal Software Engineer • Java • Python • Distributed Systems • AI/ML

Principal Software Engineer with 14+ years of experience designing and delivering large-scale distributed systems, cloud-native applications, and AI-powered platforms.

Passionate about solving complex engineering problems using strong data structures and algorithms, along with expertise in Java, Spring Boot, Python, System Design, Microservices, Cloud, Kafka, Elasticsearch, and Generative AI.

Share this Article

💬 Comments

Join the Discussion