Please use Laptop/Desktop or any other large screen for the Mock Interview Session.

Peak Index in a Mountain Array



YouTube Video Thumbnail
Link

Watch above sample mock interview video to see how it works.
Login and Buy Premium to Start the Interview



Find Peak Position in a Mountain Array

Find Peak Position in a Mountain Array

Consider an array nums to be a mountain array if it satisfies the following conditions:

  • nums.length >= 3
  • There exists an index 0 < j < nums.length - 1 such that nums[0] < nums[1] < ... < nums[j] and nums[j] > nums[j+1] > ... > nums[nums.length - 1]

Given an array that is guaranteed to be a mountain, find any index j that represents the peak element where the array first ascends and then descends.

Examples

Example 1:

Input: [1, 3, 2]
Output: 1

Example 2:

Input: [2, 5, 4, 1]
Output: 1

Constraints

  • 3 ≤ nums.length ≤ 10000
  • 0 ≤ nums[i] ≤ 100000
  • nums is guaranteed to be a mountain array.