Valid Mountain Array

Given an array of integers arr, return true if and only if it is a valid mountain array: arr.length >= 3, and there is some index i with 0 < i < arr.length - 1 such that arr[0] < arr[1] < ... < arr[i] and arr[i] > arr[i + 1] > ... > arr[arr.length - 1].

30:00

Valid Mountain Array
easy
Free written solution
Topics
Companies

Given an array of integers arr, return true if and only if it is a valid mountain array: arr.length >= 3, and there is some index i with 0 < i < arr.length - 1 such that arr[0] < arr[1] < ... < arr[i] and arr[i] > arr[i + 1] > ... > arr[arr.length - 1].

Example 1:
Input: {"arr":[2,1]}
Output: false
Constraints:
  • 1≤arr.length≤1041 \leq \text{arr.length} \leq 10^4

  • 0≤arr[i]≤1040 \leq \text{arr}[i] \leq 10^4

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"arr":[2,1]}

One climber starts at the left end (i = 0) and walks uphill; another starts at the right end (j = 1) and walks uphill toward the left.

2011i=0j=1

left climber i = 0

not started

right climber j = 1

waiting

0 < i   i == j   j < n-1

Variables
VariableValue
n2
i0
j1
DepthFunction Call
Stack empty
0/4