JuniorArray

Valid Mountain Array

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]
Example 1
#
# #
2 1
Inputarr = [2,1]
Outputfalse
The array has fewer than three elements, so it cannot be a valid mountain array.
Example 2
  # #
  # #
# # #
# # #
# # #
3 5 5
Inputarr = [3,5,5]
Outputfalse
The array does not strictly increase before decreasing because two adjacent peak values are equal.

Constraints

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

Asked at 7 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate