Valid Mountain Array - Problem

Given an array of integers arr, return true if and only if it is a valid mountain array.

Recall that arr is a mountain array if and only if:

  • arr.length >= 3
  • There exists some i with 0 < i < arr.length - 1 such that:
    • arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
    • arr[i] > arr[i + 1] > ... > arr[arr.length - 1]

This means the array must strictly increase to a peak, then strictly decrease.

Input & Output

Example 1 — Valid Mountain
$ Input: arr = [0,1,2,3,4,5,4,3,2,1,0]
Output: true
💡 Note: This array strictly increases from 0 to 5, then strictly decreases from 5 to 0, forming a valid mountain with peak at index 5.
Example 2 — No Peak
$ Input: arr = [2,1]
Output: false
💡 Note: Array length is less than 3, so cannot be a mountain. Also, it only decreases without any increase.
Example 3 — Only Increasing
$ Input: arr = [1,2,3,4,5]
Output: false
💡 Note: Array only increases without any decreasing part. A mountain must have both uphill and downhill sections.

Constraints

  • 1 ≤ arr.length ≤ 104
  • 0 ≤ arr[i] ≤ 104

Visualization

Tap to expand
Valid Mountain Array: Must Increase Then Decrease✓ Valid Mountain13541✗ Invalid (Plateau)1331Requirements:• Length ≥ 3• Strict increase to peak• Strict decrease from peak• Peak not at boundariesLeft: true, Right: false
Understanding the Visualization
1
Input Array
Given array that may or may not be a mountain
2
Check Pattern
Must strictly increase to peak, then strictly decrease
3
Output
Return true if valid mountain, false otherwise
Key Takeaway
🎯 Key Insight: A mountain has exactly one peak with no plateaus or multiple peaks
Asked in
Google 15 Amazon 12
28.0K Views
Medium Frequency
~15 min Avg. Time
950 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