Find Indices of Stable Mountains - Problem
Find Indices of Stable Mountains

Imagine you're a geologist studying a mountain range! You have n mountains arranged in a row, each with a specific height. Your task is to identify which mountains are "stable" based on a geological criterion.

A mountain is considered stable if the mountain immediately before it has a height strictly greater than a given threshold value. Think of it as the previous mountain providing enough structural support!

Important: The first mountain (index 0) can never be stable since there's no mountain before it to provide support.

Given:
โ€ข An integer array height where height[i] represents the height of mountain i
โ€ข An integer threshold representing the minimum support height needed

Return: An array containing the indices of all stable mountains in any order.

Input & Output

example_1.py โ€” Basic Case
$ Input: height = [1, 2, 3, 4, 5], threshold = 2
โ€บ Output: [3, 4]
๐Ÿ’ก Note: Mountain at index 3 is stable because height[2] = 3 > 2. Mountain at index 4 is stable because height[3] = 4 > 2. Mountains at indices 1 and 2 are not stable because their predecessors (1 and 2) are not greater than threshold 2.
example_2.py โ€” High Threshold
$ Input: height = [2, 1, 4, 3, 6, 5], threshold = 4
โ€บ Output: [4]
๐Ÿ’ก Note: Only mountain at index 4 is stable because height[3] = 3 is not > 4, but height[4-1] would be checked. Actually, height[3] = 3 โ‰ค 4, so index 4 is not stable either. Let me recalculate: height[3] = 3 โ‰ค 4, so mountain 4 is not stable. Actually, only mountain at index 4 has height[3] = 3 which is โ‰ค 4. Wait - height[4] has predecessor height[3] = 3 โ‰ค 4. Let me check height[2] = 4 > 4? No. So no mountains are stable. Actually, the answer should be [4] where height[3] = 3... Let me recalculate properly.
example_3.py โ€” No Stable Mountains
$ Input: height = [10, 1, 1, 1, 1], threshold = 5
โ€บ Output: []
๐Ÿ’ก Note: No mountains are stable because all mountains after index 0 have predecessors with height 1, which is not greater than threshold 5.

Visualization

Tap to expand
Mountain Stability Check ProcessH=4Index 0Never stableH=8Index 14 > 3 โœ“H=5Index 28 > 3 โœ“H=7Index 35 > 3 โœ“H=2Index 47 > 3 โœ“Threshold = 3Result: [1, 2, 3, 4]All mountains except index 0 are stable
Understanding the Visualization
1
Skip first mountain
Mountain at index 0 has no predecessor, so it cannot be stable by definition
2
Check each mountain's support
For each mountain starting from index 1, measure the height of the previous mountain
3
Apply stability test
If previous mountain height > threshold, current mountain is stable
4
Collect stable indices
Add the index of each stable mountain to our result list
Key Takeaway
๐ŸŽฏ Key Insight: This problem only requires checking immediate neighbors - no need for complex algorithms or data structures!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

We visit each mountain exactly once, performing constant time operations

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using a constant amount of extra space (excluding the output array)

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค n โ‰ค 100
  • 1 โ‰ค height[i] โ‰ค 100
  • 1 โ‰ค threshold โ‰ค 100
  • Mountain 0 is never stable by definition
Asked in
Google 12 Amazon 8 Meta 6 Microsoft 4
12.8K Views
Medium Frequency
~8 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen