Watch 10 video solutions for Find Indices of Stable Mountains, a easy level problem involving Array. This walkthrough by CodeJulian has 1,461 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
There are n mountains in a row, and each mountain has a height. You are given an integer array height where height[i] represents the height of mountain i, and an integer threshold.
A mountain is called stable if the mountain just before it (if it exists) has a height strictly greater than threshold. Note that mountain 0 is not stable.
Return an array containing the indices of all stable mountains in any order.
Example 1:
Input: height = [1,2,3,4,5], threshold = 2
Output: [3,4]
Explanation:
height[2] == 3 is greater than threshold == 2.height[3] == 4 is greater than threshold == 2.Example 2:
Input: height = [10,1,10,1,10], threshold = 3
Output: [1,3]
Example 3:
Input: height = [10,1,10,1,10], threshold = 10
Output: []
Constraints:
2 <= n == height.length <= 1001 <= height[i] <= 1001 <= threshold <= 100Problem Overview: You receive an integer array height representing mountain heights and a value threshold. A mountain is considered stable if its height is greater than the threshold and also strictly greater than the height immediately before it. The task is to return all indices that satisfy this stability condition.
Approach 1: Iterative Linear Scan (O(n) time, O(1) space)
The most direct solution is a single pass through the array. Start from index 1 because the first element has no previous mountain to compare against. For each index i, check two conditions: height[i] > threshold and height[i] > height[i-1]. When both conditions are true, append the index to the result list. This works because each mountain only depends on its immediate neighbor, so no extra data structures are required. The algorithm performs one comparison with the threshold and one comparison with the previous element for every index, leading to O(n) time and constant auxiliary space.
This pattern appears frequently in array problems where decisions depend on adjacent elements. A simple loop with constant memory keeps the implementation clean and efficient.
Approach 2: Functional Programming (Map/Filter) (O(n) time, O(n) space)
Languages like Python and JavaScript allow a more declarative approach using functional utilities such as filter, map, or list comprehensions. Instead of explicitly managing a loop, you generate candidate indices and filter them based on the stability condition. The filtering predicate checks the same two rules: the mountain height must exceed the threshold and be greater than the previous height.
Although the underlying complexity remains O(n) time, functional pipelines typically allocate intermediate structures or iterate over generated sequences, which can lead to O(n) auxiliary space in practice. This approach is concise and expressive but less memory-efficient than the direct loop. It still relies on the same adjacency comparison pattern common in array traversal and simple conditional filtering.
Recommended for interviews: The iterative linear scan is what interviewers expect. It shows you recognize that only the previous element matters, so the problem reduces to a single pass with constant extra space. Mentioning the functional version demonstrates familiarity with modern language features, but the loop-based solution is clearer and closer to the optimal implementation.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Linear Scan | O(n) | O(1) | Best general solution. Single pass over the array with constant memory. |
| Functional Map/Filter | O(n) | O(n) | When writing concise Python or JavaScript code using functional style utilities. |