Determine whether an array rises strictly to a single peak and then falls strictly, forming a mountain shape.
Valid Mountain Array
Given an integer array arr, determine whether it forms a valid mountain array.
An array is a valid mountain array if:
- its length is at least 3,
- there exists an index
peakwith0 < peak < n - 1, - the values strictly increase from
arr[0]toarr[peak], and - the values strictly decrease from
arr[peak]toarr[n - 1].
Return true if the array is a valid mountain array, otherwise return false.
A valid mountain must have exactly one peak and no plateaus.
Input Format
- An integer array
arr.
Output Format
- Return
trueifarris a valid mountain array; otherwise returnfalse.
Constraints
- The array must have exactly one peak.
- Adjacent values must be strictly increasing before the peak and strictly decreasing after the peak.
Example 1
Input
arr = [0,3,2,1]
Output
true
Explanation
The array strictly increases from 0 to 3, then strictly decreases from 3 to 1, with a single peak at index 1.
Example 2
Input
arr = [3,5,5]
Output
false
Explanation
The values do not strictly increase and then strictly decrease because 5 is repeated, creating a plateau.
Show 1 more example
Example 3
Input
arr = [2,1]
Output
false
Explanation
The array is too short to form a mountain.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.