FastPrepFind a Peak Element
Problem · Array

Find a Peak Element

Learn this problem
MediumBlinkit logoBlinkitFULLTIMEONSITE INTERVIEW

Problem statement

Given an integer array nums, return the index of its peak element. A peak is strictly greater than each neighbor that exists.

For this exercise, assume a missing neighbor beyond either endpoint has value negative infinity, adjacent values are different, and the array has exactly one peak. Your algorithm should run in O(log n) time.

Function

findPeakElement(nums: int[]) → int

Examples

Example 1

nums = [1,2,3,1]return = 2

The value 3 is greater than both adjacent values.

Example 2

nums = [9,6,2]return = 0

The left endpoint is greater than its only neighbor.

Example 3

nums = [4]return = 0

A one-element array has a peak at index 0.

Constraints

  • 1 <= nums.length <= 10^5.
  • -10^9 <= nums[i] <= 10^9.
  • nums[i] != nums[i + 1] for every valid i.
  • Exactly one index is a peak.

More Blinkit problems

drafts saved locally
public int findPeakElement(int[] nums) {
    // Write your code here.
}
nums[1,2,3,1]
expected2
checking account