Minimum Number of Removals to Make Mountain Array - Problem
Mountain Array Transformation Challenge

Imagine you have an array of integers that you need to transform into a mountain array by removing the minimum number of elements. A mountain array is a special sequence that:

• Has at least 3 elements
Strictly increases to a peak, then strictly decreases
• The peak cannot be at the first or last position

For example: [1, 3, 5, 4, 2] is a mountain (increases to 5, then decreases)

Your Goal: Given an integer array nums, find the minimum number of elements to remove to make it a valid mountain array.

Key Insight: This is essentially finding the longest mountain subsequence, then subtracting its length from the total array length!

Input & Output

example_1.py — Basic Mountain
$ Input: [1,3,1]
Output: 0
💡 Note: The array [1,3,1] is already a perfect mountain - it increases to peak 3, then decreases. No removals needed!
example_2.py — Need to Remove Elements
$ Input: [2,1,1,5,6,2,3,1]
Output: 3
💡 Note: We can form mountain [1,5,6,2,1] by removing elements at positions 0,2,6. The mountain increases from 1→5→6, then decreases 6→2→1.
example_3.py — Minimum Length Case
$ Input: [4,3,2,1,1,2,3,1]
Output: 4
💡 Note: Best mountain is [1,2,3,1] with length 4, so we remove 8-4=4 elements. The mountain goes 1→2→3→1.

Visualization

Tap to expand
Mountain Formation: [2,1,1,5,6,2,3,1]Original Array:2pos 01pos 11pos 25pos 36pos 42pos 53pos 61pos 7Optimal Mountain: [1,5,6,2,1]PEAKAnalysis:• Mountain length: 5 elements• Elements to remove: 8 - 5 = 3• Removed elements: [2] at pos 0, [1] at pos 2, [3] at pos 6✓ Valid mountain: 1 < 5 < 6 > 2 > 1Time Complexity: O(n log n) | Space Complexity: O(n)
Understanding the Visualization
1
Identify Climbing Potential
For each position, calculate how long an increasing sequence can be that ends there
2
Identify Descending Potential
For each position, calculate how long a decreasing sequence can be that starts there
3
Find the Best Peak
Combine both calculations to find positions that can serve as mountain peaks
4
Calculate Removals
Subtract the longest mountain length from the total array length
Key Takeaway
🎯 Key Insight: Transform the problem into finding the longest mountain subsequence using LIS algorithms, then subtract from total length to get minimum removals.

Time & Space Complexity

Time Complexity
⏱️
O(2^n * n)

2^n subsequences to generate, each taking O(n) time to validate

n
2n
Quadratic Growth
Space Complexity
O(n)

Space for storing current subsequence and recursion stack

n
2n
Linearithmic Space

Constraints

  • 3 ≤ nums.length ≤ 1000
  • 1 ≤ nums[i] ≤ 109
  • All elements in the final mountain must be strictly increasing then strictly decreasing
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 25
42.0K Views
Medium-High Frequency
~25 min Avg. Time
1.6K 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