/162. Find Peak Element

162. Find Peak Element

Medium
Arrays46.8% acceptance

Given a 0-indexed integer array arr, return the index of any element that is strictly greater than its immediate neighbors. For the first and last elements, consider out-of-bound neighbors as negative infinity. The array is guaranteed to have no consecutive equal elements. Implement an algorithm with O(log n) time complexity.

Example 1

Input: [7, 8, 5, 4, 3]

Output: 1

Explanation: 8 is a peak element at index 1.

Example 2

Input: [10, 20, 15, 2, 23, 90, 67]

Output: 5

Explanation: 90 is a peak element at index 5.

Example 3

Input: [3, 2, 1]

Output: 0

Explanation: 3 is a peak element at index 0.

Constraints

  • 1 <= len(arr) <= 1000
  • -231 <= arr[i] <= 231 - 1 for all i
  • arr[i] != arr[i+1] for all valid i
Python (current runtime)

Case 1

Input: [4, 6, 2, 1]

Expected: 1

Case 2

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

Expected: 3

Case 3

Input: [9, 7, 5, 3, 2]

Expected: 0

Case 4

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

Expected: 4