Design Gurus Logo
Valid Mountain Array (easy)

Problem Statement

Given an integer array arr, return true if arr is a valid mountain array. Otherwise, return false.

The array is called a valid mountain array if:

  • arr.length > 2
  • 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]

Examples

Example 1:

  • Input: arr = [4, 5, 6, 7, 8, 7, 6, 5]
  • Expected Output: true
  • Justification: The elements increase from 4 to 8 and then decrease from 8 back to 5, forming a mountain shape.

Example 2:

  • Input: arr = [1, 2, 3, 4, 5]
  • Expected Output: false
  • Justification: The elements only increase and there's no decrease after a peak, hence not forming a mountain.

Example 3:

  • Input: arr = [9, 8, 7, 6, 5]
  • Expected Output: false
  • Justification: The elements decrease from the start without an initial increase, failing to form a mountain shape.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory