PIXELBANKv9.1.0
Menu

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 assume nums[-1] = nums[n] = -infinity. If there are multiple peaks, return the index of any one.

Example:

Input:
1,2,3,1
Output:
2
Reasoning:
  • The input array is nums = [1, 2, 3, 1], and we need to find a peak element, which is an element strictly greater than its neighbors.
  • We compare each element with its neighbors:
    • nums[0] = 1 is not greater than nums[1] = 2,
    • nums[1] = 2 is not greater than nums[2] = 3,
    • nums[2] = 3 is greater than both nums[1] = 2 and nums[3] = 1.
  • Since nums[2] = 3 is a peak element, we return its index, which is 22.
  • The final output is the index of the peak element, which is 2\boxed{2}.

Constraints:

  • 1 <= len(nums) <= 1000
  • -2^31 <= nums[i] <= 2^31 - 1
  • nums[i] != nums[i + 1] for all valid i
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.