bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 852. Peak Index in a Mountain Array

852. Peak Index in a Mountain Array

Leetcode Solutions

Binary Search Approach

  1. Initialize two pointers l (left) and r (right) to the start and end of the array, respectively.
  2. Enter a loop that continues while l < r.
  3. Calculate the middle index mid as (l + r) / 2.
  4. If arr[mid] < arr[mid + 1], the peak must be to the right of mid, so set l to mid + 1.
  5. Otherwise, the peak is at mid or to the left of mid, so set r to mid.
  6. When l equals r, the peak has been found, and l (or r) is the peak index.
UML Thumbnail

Linear Scan Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...