ExamAdda Logo

Find Peak Element

Easy

A peak element is an element that is strictly greater than its neighbors.

Given an integer array nums, find a peak element and return its index.

You may imagine that nums[-1] = nums[n] = -∞.

This means an element can be a peak even if it is at the beginning or end of the array.

You must solve the problem in O(log n) time.

Example 1

Input

n = 4
nums = [1, 2, 3, 1]

Output

2

Explanation

nums[2] = 3 is greater than both of its neighbors:

2 < 3 > 1

 

Therefore, index 2 is a peak.

Example 2

Input

n = 5
nums = [1, 2, 1, 3, 5]

Output

1

Explanation

nums[1] = 2 is a peak because:

1 < 2 > 1

 

Index 1 is therefore a valid answer.

Constraints

1 <= nums.length <= 1000
-2^31 <= nums[i] <= 2^31 - 1
nums[i] != nums[i + 1] for all valid i
A peak element is guaranteed to exist.

Hints:

Hint 1

Compare the middle element with the element immediately to its right.

Hint 2

If:

nums[mid] < nums[mid + 1]

 

then a peak must exist somewhere to the right.

Otherwise, a peak exists at mid or somewhere to the left.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 22, 2026

Last updated

Sep 15, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output