KatInterview

875. Longest Mountain in 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

The problem asks for the length of the longest contiguous subarray in a given array that is a "mountain". A mountain array has specific properties: its length must be at least 3, and it must consist of a strictly increasing sequence followed by a strictly decreasing sequence. For example, [1, 4, 7, 3, 2] is a mountain. Our goal is to find the longest such subarray and return its length. If no such subarray exists, we should return 0.

Solution

Longest Mountain in Array in Python

class Solution:
    def longestMountain(self, arr: List[int]) -> int:
        n = len(arr)
        max_length = 0
        i = 0
        while i < n:
            end = i
            # Check for an uphill segment.
            # The start of a mountain must be an uphill slope.
            if end + 1 < n and arr[end] < arr[end + 1]:
                # Climb up to the peak.
                while end + 1 < n and arr[end] < arr[end + 1]:
                    end += 1
                
                # Now `end` is at the peak. Check for a downhill segment.
                if end + 1 < n and arr[end] > arr[end + 1]:
                    # Climb down to the end of the mountain.
                    while end + 1 < n and arr[end] > arr[end + 1]:
                        end += 1
                    # A valid mountain requires both uphill and downhill.
                    # The length is from the start `i` to the end `end`.
                    max_length = max(max_length, end - i + 1)

            # Move to the next potential starting point.
            # If a mountain or an uphill sequence was found, `end` will be greater than `i`.
            # We can continue the search from `end`.
            # If `end` did not move (`end == i`), it means arr[i] is not an uphill start.
            # We simply move to the next element `i+1`.
            # `max(end, i + 1)` correctly handles both cases.
            i = max(end, i + 1)
            
        return max_length

Longest Mountain in Array in Java

class Solution {
    public int longestMountain(int[] arr) {
        int n = arr.length;
        int maxLength = 0;
        int i = 0;
        while (i < n) {
            int end = i;
            // Check for an uphill segment.
            // The start of a mountain must be an uphill slope.
            if (end + 1 < n && arr[end] < arr[end + 1]) {
                // Climb up to the peak.
                while (end + 1 < n && arr[end] < arr[end + 1]) {
                    end++;
                }
                
                // Now `end` is at the peak. Check for a downhill segment.
                if (end + 1 < n && arr[end] > arr[end + 1]) {
                    // Climb down to the end of the mountain.
                    while (end + 1 < n && arr[end] > arr[end + 1]) {
                        end++;
                    }
                    // A valid mountain requires both uphill and downhill.
                    // The length is from the start `i` to the end `end`.
                    maxLength = Math.max(maxLength, end - i + 1);
                }
            }
            
            // Move to the next potential starting point.
            // If a mountain or an uphill sequence was found, `end` will be greater than `i`.
            // We can continue the search from `end`.
            // If `end` did not move (`end == i`), it means arr[i] is not an uphill start.
            // We simply move to the next element `i+1`.
            // `Math.max(end, i + 1)` correctly handles both cases.
            i = Math.max(end, i + 1);
        }
        return maxLength;
    }
}

Longest Mountain in Array in C++

class Solution {
public:
    int longestMountain(vector<int>& arr) {
        int n = arr.size();
        int maxLength = 0;
        int i = 0;
        while (i < n) {
            int end = i;
            // Check for an uphill segment.
            // The start of a mountain must be an uphill slope.
            if (end + 1 < n && arr[end] < arr[end + 1]) {
                // Climb up to the peak.
                while (end + 1 < n && arr[end] < arr[end + 1]) {
                    end++;
                }
                
                // Now `end` is at the peak. Check for a downhill segment.
                if (end + 1 < n && arr[end] > arr[end + 1]) {
                    // Climb down to the end of the mountain.
                    while (end + 1 < n && arr[end] > arr[end + 1]) {
                        end++;
                    }
                    // A valid mountain requires both uphill and downhill.
                    // The length is from the start `i` to the end `end`.
                    maxLength = std::max(maxLength, end - i + 1);
                }
            }
            
            // Move to the next potential starting point.
            // If a mountain or an uphill sequence was found, `end` will be greater than `i`.
            // We can continue the search from `end`.
            // If `end` did not move (`end == i`), it means arr[i] is not an uphill start.
            // We simply move to the next element `i+1`.
            // `std::max(end, i + 1)` correctly handles both cases.
            i = std::max(end, i + 1);
        }
        return maxLength;
    }
};

Longest Mountain in Array in TypeScript

function longestMountain(arr: number[]): number {
    const n = arr.length;
    let maxLength = 0;
    let i = 0;
    while (i < n) {
        let end = i;
        // Check for an uphill segment.
        // The start of a mountain must be an uphill slope.
        if (end + 1 < n && arr[end] < arr[end + 1]) {
            // Climb up to the peak.
            while (end + 1 < n && arr[end] < arr[end + 1]) {
                end++;
            }
            
            // Now `end` is at the peak. Check for a downhill segment.
            if (end + 1 < n && arr[end] > arr[end + 1]) {
                // Climb down to the end of the mountain.
                while (end + 1 < n && arr[end] > arr[end + 1]) {
                    end++;
                }
                // A valid mountain requires both uphill and downhill.
                // The length is from the start `i` to the end `end`.
                maxLength = Math.max(maxLength, end - i + 1);
            }
        }
        
        // Move to the next potential starting point.
        // If a mountain or an uphill sequence was found, `end` will be greater than `i`.
        // We can continue the search from `end`.
        // If `end` did not move (`end == i`), it means arr[i] is not an uphill start.
        // We simply move to the next element `i+1`.
        // `Math.max(end, i + 1)` correctly handles both cases.
        i = Math.max(end, i + 1);
    }
    return maxLength;
};

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.