Skip to content

Commit 5387f9e

Browse files
authored
Merge pull request #2121 from Saipradyumnagoud/main
Added 941-valid-mountain-array.
2 parents f69f361 + 0c76994 commit 5387f9e

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
id: valid-mountain-array
3+
title: Valid Mountain Array
4+
level: easy
5+
sidebar_label: Valid Mountain Array
6+
tags:
7+
- Array
8+
description: "This document provides a solution for the Valid Mountain Array problem."
9+
---
10+
11+
## Problem Statement
12+
13+
Given an array of integers `arr`, return `true` if and only if it is a valid mountain array.
14+
15+
Recall that `arr` is a mountain array if and only if:
16+
17+
- `arr.length >= 3`
18+
- There exists some `i` with `0 < i < arr.length - 1` such that:
19+
- `arr[0] < arr[1] < ... < arr[i - 1] < arr[i]`
20+
- `arr[i] > arr[i + 1] > ... > arr[arr.length - 1]`
21+
22+
**Example 1:**
23+
24+
Input: `arr = [2, 1]`
25+
26+
Output: `false`
27+
28+
**Example 2:**
29+
30+
Input: `arr = [3, 5, 5]`
31+
32+
Output: `false`
33+
34+
**Example 3:**
35+
36+
Input: `arr = [0, 3, 2, 1]`
37+
38+
Output: `true`
39+
40+
**Constraints:**
41+
42+
- `1 <= arr.length <= 10^4`
43+
- `0 <= arr[i] <= 10^4`
44+
45+
## Solutions
46+
47+
### Approach
48+
49+
To determine if the given array is a valid mountain array, follow these steps:
50+
51+
1. **Initial Check**: Ensure the length of the array is at least 3.
52+
2. **Increasing Phase**: Traverse the array from the start, checking if each element is less than the next one.
53+
3. **Peak Check**: If the peak is at the beginning or end of the array, it's not a valid mountain.
54+
4. **Decreasing Phase**: Continue traversing from the peak, checking if each element is greater than the next one.
55+
5. **Final Check**: Ensure that the entire array was traversed.
56+
57+
### Java
58+
59+
```java
60+
class Solution {
61+
public boolean validMountainArray(int[] arr) {
62+
int len = arr.length;
63+
if (len < 3) {
64+
return false;
65+
}
66+
67+
boolean flag = true;
68+
for (int i = 0; i < len - 1; i++) {
69+
if (arr[i] == arr[i + 1]) {
70+
return false;
71+
}
72+
if (flag && arr[i + 1] < arr[i]) {
73+
if (i == 0) return false;
74+
flag = false;
75+
}
76+
if (!flag && arr[i] < arr[i + 1]) {
77+
return false;
78+
}
79+
}
80+
if (flag) {
81+
return false;
82+
}
83+
return true;
84+
}
85+
}
86+
87+
```
88+
89+
### Python
90+
```Python
91+
class Solution:
92+
def validMountainArray(self, arr: List[int]) -> bool:
93+
length = len(arr)
94+
if length < 3:
95+
return False
96+
97+
flag = True
98+
for i in range(length - 1):
99+
if arr[i] == arr[i + 1]:
100+
return False
101+
if flag and arr[i + 1] < arr[i]:
102+
if i == 0:
103+
return False
104+
flag = False
105+
if not flag and arr[i] < arr[i + 1]:
106+
return False
107+
108+
if flag:
109+
return False
110+
111+
return True
112+
```

0 commit comments

Comments
 (0)