Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

60. Find Peak Element

MediumDay 18 of 60Binary Search

A peak is an element strictly greater than its neighbours. Given an array nums where no two adjacent values are equal, return the index of any peak. Treat the positions just outside the array as negative infinity, so the first or last element can be a peak. Aim for O(log n).

Example 1
Input: nums = [1,2,3,1]
Output: 2
3 is bigger than 2 and 1.
Example 2
Input: nums = [1,2,1,3,5,6,4]
Output: 5
Index 1 (value 2) is also a peak, so 1 is accepted too.
Constraints
  • 1 <= nums.length <= 1000
  • -2^31 <= nums[i] <= 2^31 - 1
  • `nums[i] != nums[i + 1]` for every valid i
Hints
Frequently asked at: Meta, Google, Amazon, Microsoft
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min