# Find Indices of Stable Mountains
**Difficulty:** EASY
[External](https://leetcode.com/problems/find-indices-of-stable-mountains)
Canonical: https://scaleengineer.com/dsa/problems/find-indices-of-stable-mountains
**Data structures:** Array
---
## 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** 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:**

* Mountain 3 is stable because `height[2] == 3` is greater than `threshold == 2`.
* Mountain 4 is stable because `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 <= 100`
* `1 <= height[i] <= 100`
* `1 <= threshold <= 100`

# Approaches
## Brute-Force Iteration
This approach directly translates the problem definition into code. We iterate through each mountain from the very first one (index 0) to the last, and for each mountain, we check if it satisfies the conditions to be considered 'stable'.
**Time:** O(n), where n is the number of mountains. We iterate through the entire array once. · **Space:** O(k), where k is the number of stable mountains. In the worst case, k can be up to n-1, making the space complexity O(n) to store the result list.
**Pros:** Very simple to understand as it directly models the problem statement.; Correct and handles all edge cases.
**Cons:** Slightly inefficient as it starts the loop from index 0 and performs a check (`i > 0`) in every iteration, even though we know mountain 0 can never be stable.
### Explanation
The algorithm involves a single loop that traverses the entire `height` array. For each mountain at index `i`, we perform two checks:
1.  **Existence of a preceding mountain:** We check if `i > 0`. This is crucial because mountain 0 has no preceding mountain and is explicitly defined as not stable.
2.  **Height of the preceding mountain:** If a preceding mountain exists (at index `i-1`), we check if its height, `height[i-1]`, is strictly greater than the given `threshold`.

If both conditions are true, the index `i` is added to a result list. This list is then returned after the loop completes.

```java
import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<Integer> findStableMountains(int[] height, int threshold) {
        List<Integer> stableIndices = new ArrayList<>();
        int n = height.length;
        for (int i = 0; i < n; i++) {
            // Check if a preceding mountain exists and its height is > threshold
            if (i > 0 && height[i - 1] > threshold) {
                stableIndices.add(i);
            }
        }
        return stableIndices;
    }
}
```
### Algorithm
1. Initialize an empty list, `result`, to store the indices of stable mountains.
2. Iterate through the `height` array with an index `i` from `0` to `n-1`, where `n` is the length of the array.
3. Inside the loop, check if `i > 0` to ensure a preceding mountain exists.
4. If `i > 0`, then check if `height[i-1]` is strictly greater than `threshold`.
5. If both conditions are met, add the current index `i` to the `result` list.
6. After the loop finishes, return the `result` list.

## Optimized Single-Pass Iteration
This is the most efficient approach. Recognizing that mountain 0 can never be stable, we can optimize the iteration by starting our check from mountain 1. This eliminates a redundant condition check inside the loop and makes the code slightly cleaner and more direct.
**Time:** O(n), where n is the number of mountains. We iterate through the array once, from the second element to the last. · **Space:** O(k), where k is the number of stable mountains. In the worst case, k can be up to n-1, making the space complexity O(n) for the output list.
**Pros:** Optimal time and space complexity.; Code is clean, concise, and avoids redundant checks.; Most direct and efficient implementation.
**Cons:** There are no significant drawbacks to this approach; it is the ideal solution for this problem.
### Explanation
The core idea is to skip checking mountain 0 altogether. We know a mountain `i` is stable only if the mountain at `i-1` exists and its height is above the threshold. This is only possible for `i >= 1`.

Therefore, we can start our loop directly from `i = 1` and iterate up to `n-1`. For each `i`, the preceding mountain `i-1` is guaranteed to exist. We only need to perform one check: `height[i-1] > threshold`. If this condition holds, we add `i` to our result list.

This approach is optimal as it requires visiting each relevant element of the array exactly once.

```java
import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<Integer> findStableMountains(int[] height, int threshold) {
        List<Integer> stableIndices = new ArrayList<>();
        int n = height.length;
        // Start from index 1, as mountain 0 cannot be stable.
        for (int i = 1; i < n; i++) {
            // Check if the previous mountain's height is greater than the threshold.
            if (height[i - 1] > threshold) {
                stableIndices.add(i);
            }
        }
        return stableIndices;
    }
}
```
### Algorithm
1. Initialize an empty list, `result`, to store the indices of stable mountains.
2. Iterate through the `height` array with an index `i` from `1` to `n-1`.
3. Inside the loop, check if the height of the preceding mountain, `height[i-1]`, is strictly greater than `threshold`.
4. If the condition is true, add the current index `i` to the `result` list.
5. After the loop finishes, return the `result` list.

# Solutions
### Java

```java
class Solution {
public
  List<Integer> stableMountains(int[] height, int threshold) {
    List<Integer> ans = new ArrayList<>();
    for (int i = 1; i < height.length; ++i) {
      if (height[i - 1] > threshold) {
        ans.add(i);
      }
    }
    return ans;
  }
}

```

### CPP

```cpp
class Solution {
public:
  vector<int> stableMountains(vector<int> &height, int threshold) {
    vector<int> ans;
    for (int i = 1; i < height.size(); ++i) {
      if (height[i - 1] > threshold) {
        ans.push_back(i);
      }
    }
    return ans;
  }
};

```

### Python

```python
class Solution:
    def stableMountains(self, height: List[int], threshold: int) -> List[int]: return [
        i for i in range(1, len(height)) if height[i - 1] > threshold]

```
