Skip to main content

Find Indices of Stable Mountains - Solution & Explanation

EasyArray11 min readAsked at: Google
Practice this problem

Problem Statement

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

Approach Overview

Problem 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 1: Iterative Approach

This approach involves iterating through the heights starting from the second element (index 1) since the first mountain cannot be stable. For each mountain at index i, check if the height of the previous mountain (height[i-1]) is greater than the given threshold. If it is, add the current index i to the list of stable mountains.

This C solution allocates memory for the resultant array and iterates through the heights array from index 1. For each element, it checks if the previous element is greater than the threshold. If so, it appends the index to the result. Finally, it prints the stable mountain indices and frees the allocated memory.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the number of mountains.
Space Complexity: O(n) for storing the result.

Try this approach in the editor →

Approach 2: Functional Programming Approach (Using Map/Filter)

This approach leverages functional programming paradigms, using mapping and filtering to achieve the same results more concisely, as supported by the language. We create mappings of possible indices and filter them according to our stability condition.

In this Python solution, we use the filter function along with a lambda to iterate over index range from 1 to len(height), keeping only those indices where the previous mountain's height is greater than the threshold.

Code

Python

JavaScript

Complexity

Time Complexity: O(n).
Space Complexity: O(n).

Try this approach in the editor →

Approach 3: Traversal

We directly traverse the mountains starting from index 1. If the height of the mountain to its left is greater than threshold, we add its index to the result array.

After the traversal, we return the result array.

The time complexity is O(n), where n is the length of the array height. Ignoring the space consumption of the result array, the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Iterative Approach

Time Complexity: O(n), where n is the number of mountains.
Space Complexity: O(n) for storing the result.

Functional Programming Approach (Using Map/Filter)

Time Complexity: O(n).
Space Complexity: O(n).

Traversal—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterative Linear ScanO(n)O(1)Best general solution. Single pass over the array with constant memory.
Functional Map/FilterO(n)O(n)When writing concise Python or JavaScript code using functional style utilities.

Video Solution

Leetcode | 3285. Find Indices of Stable Mountains | Easy | Java Solution • Developer Docs • 662 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Find Indices of Stable Mountains easy or hard?
The problem is classified as Easy on LeetCode with a high acceptance rate. It focuses on recognizing a simple adjacency condition in an array and implementing an efficient single-pass scan.
Find Indices of Stable Mountains Python/Java solution
The solution in Python or Java follows the same logic: iterate from index 1 to n-1, check the stability condition, and store valid indices in a result list. Python can also implement the same logic using list comprehensions or filter functions.
How to solve Find Indices of Stable Mountains in O(n)?
Iterate through the array starting at index 1. At each position check whether the current height exceeds the threshold and is greater than the previous height. If both conditions are satisfied, record the index. Because each element is processed once, the algorithm runs in O(n) time.
What is the best approach for Find Indices of Stable Mountains?
The best approach is a single-pass iterative scan of the array. For each index i starting from 1, check whether height[i] is greater than the threshold and greater than height[i-1]. If both conditions hold, add the index to the result. This solution runs in O(n) time and uses O(1) extra space.
Is Find Indices of Stable Mountains asked at Google/Amazon/Meta?
Problems like this appear frequently in screening rounds because they test basic array traversal and conditional logic. While this exact question is categorized as easy, similar array scanning patterns are common in interviews at companies such as Amazon, Google, and Meta.
What data structure is used in Find Indices of Stable Mountains?
The problem primarily uses a simple array traversal. No advanced data structures are required since each decision depends only on the current element and its immediate predecessor.
What is the time complexity of Find Indices of Stable Mountains?
The optimal solution runs in O(n) time because the array is traversed exactly once. Each step performs constant-time comparisons with the threshold and the previous element. Space complexity is O(1) excluding the output list of indices.

Ready to solve this problem?

Practice Find Indices of Stable Mountains with our built-in code editor and test cases.

Practice on FleetCode