# LC 941. Valid Mountain Array ### [Problem link](https://leetcode.com/problems/valid-mountain-array/) ###### tags: `leedcode` `python` `c++` `easy` `Two Pointer` Given an array of integers <code>arr</code>, return <code>true</code> if and only if it is a valid mountain array. Recall that arr is a mountain array if and only if: - <code>arr.length >= 3</code> - There exists some <code>i</code> with <code>0 < i < arr.length - 1</code> such that: - <code>arr[0] < arr[1] < ... < arr[i - 1] < arr[i] </code> - <code>arr[i] > arr[i + 1] > ... > arr[arr.length - 1]</code> <img src="https://assets.leetcode.com/uploads/2019/10/20/hint_valid_mountain_array.png" width="500" /> **Example 1:** ```Input: arr = [2,1] Output: false ```**Example 2:** ```Input: arr = [3,5,5] Output: false ```**Example 3:** ```Input: arr = [0,3,2,1] Output: true ``` **Constraints:** - <code>1 <= arr.length <= 10<sup>4</sup></code> - <code>0 <= arr[i] <= 10<sup>4</sup></code> ## Solution 1 - Two Pointer #### Python ```python= class Solution: def validMountainArray(self, arr: List[int]) -> bool: n = len(arr) left, right = 0, n - 1 while left <= n - 2 and arr[left] < arr[left + 1]: left += 1 while right >= 1 and arr[right - 1] > arr[right]: right -= 1 if left == n - 1 or right == 0: return False return left == right ``` #### C++ ```cpp= class Solution { public: bool validMountainArray(vector<int>& arr) { if (arr.size() < 3) { return false; } int left = 0; int right = arr.size() - 1; while (left < arr.size() - 1 && arr[left] < arr[left + 1]) { left++; } while (right > 0 && arr[right - 1] > arr[right]) { right--; } if (left == 0 || right == arr.size() - 1 || left != right) { return false; } return true; } }; ``` >### Complexity >n = arr.length >| | Time Complexity | Space Complexity | >| ----------- | --------------- | ---------------- | >| Solution 1 | O(n) | O(1) | ## Note x