Mountain Array
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 index
i (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]
Examples
isMountainArray([1,4,7,9,8,5,3]) // returns true // The array forms a mountain with a peak at arr[3] isMountainArray([5,8,12,12,9,7]) // returns false // The array fails to form a mountain because // arr[2] == arr[3], indicating a flat segment isMountainArray([5,6,5,4,3]) // returns true // The array forms a mountain with a peak at arr[1]
Loading...