Find Indices of Stable Mountains - Problem

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: Mountain 0 is not stable.

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

Input & Output

Example 1 — Basic Case
$ Input: height = [1,2,3,4,5], threshold = 2
Output: [3,4]
💡 Note: Mountain 3 is stable because height[2] = 3 > 2. Mountain 4 is stable because height[3] = 4 > 2. Mountains 0,1,2 are not stable.
Example 2 — No Stable Mountains
$ Input: height = [2,1,1], threshold = 5
Output: []
💡 Note: No mountain is stable because height[0] = 2 ≤ 5 and height[1] = 1 ≤ 5.
Example 3 — All Mountains Stable
$ Input: height = [10,1,10,1,10], threshold = 3
Output: [1,2,3,4]
💡 Note: Mountains 1,2,3,4 are all stable because their previous mountains have height 10 > 3.

Constraints

  • 1 ≤ height.length ≤ 100
  • 1 ≤ height[i] ≤ 100
  • 1 ≤ threshold ≤ 100

Visualization

Tap to expand
Find Stable Mountains: height = [1,2,3,4,5], threshold = 212345idx 0idx 1idx 2idx 3idx 4Threshold = 2✓ height[2] = 3 > 2✓ height[3] = 4 > 2Mountains 3 and 4 are stable[3, 4]
Understanding the Visualization
1
Input
Array of mountain heights and threshold value
2
Process
Check each mountain's previous height against threshold
3
Output
Array of indices where mountains are stable
Key Takeaway
🎯 Key Insight: A mountain is stable if and only if its immediate predecessor has height strictly greater than the threshold
Asked in
Microsoft 15 Apple 12
8.5K Views
Medium Frequency
~10 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