BackmediumArrays Oracle Uber

Peak Element Finder in Mountain Array Solution

Problem Statement

Given an array of integers representing a mountain sequence where elements strictly increase and then strictly decrease, find the index of the peak element.

Example 1
Input
[2, 1, 4, 7, 3, 2, 5]
Output
5

Explanation: Step-by-step: with input [2, 1, 4, 7, 3, 2, 5], we first find the peak element by iterating through the array from left to right. We find that the element 7 is greater than its previous neighbor (4) and next neighbor (3), making it the peak element. Therefore, the output is 4.

Example 2
Input
[3, 2, 1, 4, 5, 2, 6, 7, 3, 2, 1]
Output
7

Explanation: Step-by-step: with input [3, 2, 1, 4, 5, 2, 6, 7, 3, 2, 1], we first find the peak element by iterating through the array from left to right. We find that the element 7 is greater than its previous neighbor (6) and next neighbor (3), making it the peak element. Therefore, the output is 7.

Constraints

  • 1 <= array length <= 10^5
  • Array represents a mountain sequence
  • All elements are distinct integers
  • Array is not empty
  • Only one peak element exists
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free