Introduction
A mountain subarray is a contiguous portion of an array that first strictly increases and then strictly decreases.
For example:
The array increases toward 7 and decreases after 7, making it a valid mountain of length 5.
The goal is to find the longest mountain subarray in the given array.
This problem is useful for understanding an important array pattern:
Find a valid peak → expand around the peak → calculate the complete structure.
The follow-up also asks for a solution using one pass and O(1) extra space, making it a good interview problem for learning how to optimize array traversal.
Problem Statement
Problem Link -: longest mountain subarray
Given an integer array arr, return the length of the longest contiguous subarray that forms a mountain.
A valid mountain must:
- Contain at least
3elements. - Strictly increase toward a peak.
- Strictly decrease after the peak.
- Have the peak somewhere between the first and last element.
For example:
The longest mountain is:
Therefore:
Understanding the Pattern
The most important observation is that every valid mountain has a peak.
A peak is an element satisfying:
For example:
At index 2:
Therefore, 7 is a peak.
Once a peak is found, the complete mountain can be discovered by expanding:
The left side continues while values are increasing toward the peak.
The right side continues while values are decreasing away from the peak.
Approach: Expand Around Every Peak
The solution can be divided into three simple steps.
Find every possible peak
Traverse the array from index 1 to n - 2.
For every index:
If this condition is true, i is a valid mountain peak.
Expand toward the left
Starting from the peak, move left while:
This finds how far the increasing portion extends.
Expand toward the right
Starting from the peak, move right while:
This finds how far the decreasing portion extends.
The peak is counted from both sides, so:
Java Solution
The following implementation follows the peak-expansion approach while keeping the extra space at O(1).
Dry Run
Consider:
Start scanning from index 1.
Index 1
1 is not a peak because:
Move forward.
Index 2
For 4:
Not a peak.
Index 3
For 7:
So 7 is a valid peak.
Expand left
Starting at 7:
The left side is:
Length:
Expand right
Starting at 7:
The right side is:
Length:
The peak 7 was counted twice:
Therefore:
Why the Peak Is Counted Twice
This is an important detail in the implementation.
Suppose the mountain is:
The left traversal counts:
The right traversal counts:
Adding them gives:
But the 7 belongs to both sections.
Therefore:
Hence:
Complexity Analysis
Time Complexity
The overall complexity is:
Although the implementation expands left and right whenever a peak is found, each increasing/decreasing section belongs to a particular mountain structure and the total traversal remains linear.
Space Complexity
Only a few integer variables are used:
No auxiliary array, HashMap, HashSet, or other data structure is required.
One-Pass Optimization
The same problem can also be solved using a direct one-pass approach.
Instead of explicitly expanding from every peak, maintain:
- the length of the current increasing section
- the length of the current decreasing section
When an increasing sequence changes into a decreasing sequence, a mountain has been formed.
A compact implementation is:
This approach directly satisfies the follow-up requirement:
Common Mistakes
Treating a simple increasing sequence as a mountain
This is not a mountain because there is no decreasing portion.
Treating a simple decreasing sequence as a mountain
This is also not a mountain because there is no increasing portion.
Allowing equal elements
This is not a valid mountain because the increase must be strict.
The conditions require:
not:
Forgetting the minimum length
A mountain must contain at least three elements:
increasing part + peak + decreasing part
Therefore:
Interview Tip
The key to this problem is not the code itself but recognizing the peak structure.
Whenever a problem describes something like:
a useful first thought is:
Can the middle element be treated as a peak and the structure be expanded from there?
This transforms a complicated subarray condition into two simple directional scans.
For interview optimization questions, it is also useful to recognize that the same structure can often be tracked using a few variables instead of repeatedly constructing subarrays.
Conclusion
The Longest Mountain in Array problem demonstrates an important array technique: identifying a structural peak and analyzing the elements around it.
The main ideas are:
- A mountain must have a valid peak.
- The left side must be strictly increasing.
- The right side must be strictly decreasing.
- Expanding from each peak provides an intuitive solution.
- The problem can also be optimized into a true one-pass O(n), O(1) solution.
The most important pattern to remember is:
Once that pattern becomes familiar, similar problems involving peaks, valleys, increasing/decreasing runs, and subarray structures become significantly easier to recognize.




