Peak Index in a Mountain Array

Updated on 01 July, 2025
Peak Index in a Mountain Array header image

Problem Statement

Given an integer array arr, it represents a mountain sequence where the numbers in the sequence increase to a maximum peak value and then begin to decrease. The task is to locate and return the index of this peak element. Unlike a traditional search which can operate in O(n) time, the solution for this problem must be optimized to run in O(log(n)) time complexity, akin to a binary search. This stringent time complexity requirement suggests that a direct linear search is insufficient, and a more sophisticated approach, leveraging the properties of the mountain sequence, is necessary.

Examples

Example 1

Input:

arr = [0,1,0]

Output:

1

Example 2

Input:

arr = [0,2,1,0]

Output:

1

Example 3

Input:

arr = [0,10,5,2]

Output:

1

Constraints

  • 3 <= arr.length <= 105
  • 0 <= arr[i] <= 106
  • arr is guaranteed to be a mountain array.

Approach and Intuition

To achieve the O(log(n)) time complexity, the problem closely mirrors the concept used in finding an element in a sorted array with a binary search. However, the twist here is that the array isn't fully sorted; it first rises to a peak and then declines. This unique structure enables the use of a modified binary search:

  1. Initialize pointers: Start with two pointers, left at the beginning and right at the end of the array.

  2. Binary search application: While left is less than right:

    • Calculate mid as the average of left and right.
    • Check the elements adjacent to arr[mid]. There are three cases:
      • If arr[mid-1] < arr[mid] > arr[mid+1], you've found your peak; return mid.
      • If arr[mid-1] < arr[mid] < arr[mid+1], this implies that the peak is still to the right because the sequence is still ascending. Adjust the left pointer to mid + 1.
      • If arr[mid-1] > arr[mid] > arr[mid+1], this means the peak is to the left, so adjust the right pointer to mid - 1.
  3. Conclusion: The loop eventually narrows down to the peak, where left equals right, the index of the peak element.

The rationale behind this approach capitalizes on the guaranteed presense of a single peak and no plateaus (i.e., no two equal maximum elements), as implies by the constraints and the definition of a mountain array. This ensures that the binary search modification effectively converges on the peak element.

Solutions

  • C++
  • Java
  • Python
cpp
class Solution {
public:
    int findMountainPeak(vector<int>& array) {
        int left = 0, right = array.size() - 1, middle;
        while (left < right) {
            middle = (left + right) / 2;
            if (array[middle] < array[middle + 1])
                left = middle + 1;
            else
                right = middle;
        }
        return left;
    }
};

The solution focuses on finding the peak index in a mountain array using C++. A mountain array is defined as an array where elements first increase to a peak where no two adjacent elements are equal, and then after the peak, the elements strictly decrease.

The approach utilized in the code is a binary search:

  • Initialize left to 0 and right to the last index of the array.
  • Implement a loop to continue while left is less than right.
  • Calculate middle as the average of left and right.
  • If the element at the middle index is less than the element at middle + 1, adjust left to be middle + 1.
  • If the above condition doesn't hold, set right to middle.
  • The loop exits when left equals right, indicating the peak index is found.

The provided function, findMountainPeak, returns the peak index left after narrowing down its possible location through iterative comparisons. This method ensures a time complexity of O(log n), leveraging the efficiency of binary search.

java
class Solution {
    public int findMountainTopIndex(int[] mountainArray) {
        int low = 0, high = mountainArray.length - 1, middle;
        while (low < high) {
            middle = (low + high) / 2;
            if (mountainArray[middle] < mountainArray[middle + 1])
                low = middle + 1;
            else
                high = middle;
        }
        return low;
    }
}

This solution helps find the peak index in a mountain array using Java. The method findMountainTopIndex accepts an array of integers representing the mountain array. A binary search technique is used to locate the peak efficiently:

  1. Initialize low to the first element index and high to the last element index of the array.
  2. Use a loop to continue searching as long as low is less than high.
  3. Calculate middle as the average of low and high to get the middle index.
  4. If the element at middle is less than its next element, adjust low to middle + 1.
  5. If not, adjust high to middle.
  6. The loop exits when low equals high, which will be the index of the peak element (the highest element in the mountain array).

The binary search ensures the approach is efficient, running in O(log n) time complexity, making it suitable for large arrays.

python
class Solution:
    def findPeakIndex(self, mountain: List[int]) -> int:
        low = 0
        high = len(mountain) - 1
        while low < high:
            middle = (low + high) // 2
            if mountain[middle] < mountain[middle + 1]:
                low = middle + 1
            else:
                high = middle
        return low

This solution summary addresses the problem of finding the peak index in a 'mountain' array, an array where elements increase up to a peak point then decrease. The implementation uses a binary search algorithm to efficiently find the peak in O(log n) time complexity due to its divide-and-conquer approach.

  • Start by initializing two pointers: low set to 0 and high set to the last index of the array.
  • Enter a while loop where you continue until low is less than high.
  • Calculate the middle index by averaging low and high.
  • Compare the element at the middle index with the element right after it (middle + 1). If the middle element is less than middle + 1, adjust low to search the upper half of the array, otherwise adjust high to continue with the lower half.
  • Exit the loop when low equals high, which is when you have found the peak index.

Final result returned is the value of low, which corresponds to the peak index in the mountain array. This solution guarantees finding the peak under the mountain array's constraints, delivering both effectiveness and efficiency.

Comments

No comments yet.