KatInterview

882. Peak Index in a Mountain Array

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

Method: Binary Search

This problem asks for the index of the peak element in a mountain array, with a required time complexity of O(log n). This complexity strongly suggests that a binary search algorithm is the intended solution.

Solution

Peak Index in a Mountain Array in Python

class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        left, right = 0, len(arr) - 1
        
        # Binary search to find the peak index.
        # The loop invariant is that the peak is always within the range [left, right].
        while left < right:
            # Standard mid calculation for binary search.
            mid = left + (right - left) // 2
            
            # If arr[mid] is less than its right neighbor, we are on the ascending slope.
            # The peak must be to the right of mid.
            if arr[mid] < arr[mid + 1]:
                left = mid + 1
            else:
                # If arr[mid] is greater than its right neighbor, mid is either the peak
                # or on the descending slope. In both cases, the peak is at or to the left of mid.
                # We set right = mid because mid could be the peak itself.
                right = mid
        
        # When the loop terminates, left and right will converge to the same index,
        # which is the peak index.
        return left

Peak Index in a Mountain Array in Java

class Solution {
    public int peakIndexInMountainArray(int[] arr) {
        int left = 0;
        int right = arr.length - 1;

        // Binary search to find the peak index.
        // The loop invariant is that the peak is always within the range [left, right].
        while (left < right) {
            // Prevent potential overflow.
            int mid = left + (right - left) / 2;

            // If arr[mid] is less than its right neighbor, we are on the ascending slope.
            // The peak must be to the right of mid.
            if (arr[mid] < arr[mid + 1]) {
                left = mid + 1;
            } else {
                // If arr[mid] is greater than its right neighbor, mid is either the peak
                // or on the descending slope. In both cases, the peak is at or to the left of mid.
                // We set right = mid because mid could be the peak itself.
                right = mid;
            }
        }

        // When the loop terminates, left and right will converge to the same index,
        // which is the peak index.
        return left;
    }
}

Peak Index in a Mountain Array in C++

class Solution {
public:
    int peakIndexInMountainArray(vector<int>& arr) {
        int left = 0;
        int right = arr.size() - 1;
        
        // Binary search to find the peak index.
        // The loop invariant is that the peak is always within the range [left, right].
        while (left < right) {
            // Prevent potential overflow, though not necessary with current constraints.
            int mid = left + (right - left) / 2;
            
            // If arr[mid] is less than its right neighbor, we are on the ascending slope.
            // The peak must be to the right of mid.
            if (arr[mid] < arr[mid + 1]) {
                left = mid + 1;
            } else {
                // If arr[mid] is greater than its right neighbor, mid is either the peak
                // or on the descending slope. In both cases, the peak is at or to the left of mid.
                // We set right = mid because mid could be the peak itself.
                right = mid;
            }
        }
        
        // When the loop terminates, left and right will converge to the same index,
        // which is the peak index.
        return left;
    }
};

Peak Index in a Mountain Array in TypeScript

function peakIndexInMountainArray(arr: number[]): number {
    let left = 0;
    let right = arr.length - 1;

    // Binary search to find the peak index.
    // The loop invariant is that the peak is always within the range [left, right].
    while (left < right) {
        // Use Math.floor for integer division.
        let mid = Math.floor(left + (right - left) / 2);

        // If arr[mid] is less than its right neighbor, we are on the ascending slope.
        // The peak must be to the right of mid.
        if (arr[mid] < arr[mid + 1]) {
            left = mid + 1;
        } else {
            // If arr[mid] is greater than its right neighbor, mid is either the peak
            // or on the descending slope. In both cases, the peak is at or to the left of mid.
            // We set right = mid because mid could be the peak itself.
            right = mid;
        }
    }

    // When the loop terminates, left and right will converge to the same index,
    // which is the peak index.
    return left;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.