Skip to content

Practice Valid Mountain Array

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#941
LeetCode Problem

Valid Mountain Array

Problem Statement

Return whether arr has length at least three and a peak strictly inside the array. Values must strictly increase from the first element to that peak and strictly decrease from the peak to the last element. Equal neighboring values, a peak at either endpoint, or a missing ascent or descent are invalid.

Source: Valid Mountain Array on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
arr = [2,1]
Output
false
Explanation

A mountain needs an ascent, an interior peak and a descent, which requires at least three elements. This array has only two. It also starts by decreasing from 2 to 1, placing its maximum at the first position rather than an interior peak. Return false before attempting a two-phase ascent/descent scan. A sequence being strictly decreasing does not make it a mountain.

# Example 2

Input
arr = [3,5,5]
Output
false
Explanation

The values rise from 3 to 5, but the next comparison is 5 = 5. A mountain requires strict inequality on both sides of a single interior peak; a flat top is invalid. Treating either 5 as the peak cannot help: one side would still contain equality or have no descent. Return false. Use strict comparisons when scanning, because non-strict comparisons would incorrectly accept this plateau.

# Example 3

Input
arr = [0,3,2,1]
Output
true
Explanation

The peak is 3 at index 1, strictly inside the four-element array. Before it, 0 < 3 gives a genuine ascent. After it, 3 > 2 > 1 gives a continuous strict descent, so return true. A scan can climb to index 1 and then descend all the way to the final element. Requiring both phases to contain a step excludes endpoint peaks; requiring the descent to reach the end excludes a second rise.

Constraints

1 <= arr.length <= 10^4
0 <= arr[i] <= 10^4

Related LeetCode Problems

Practice more problems similar to Valid Mountain Array.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.