Given an array arr[]. The task is to check whether it is a mountain array or not. A mountain array is an array of length at least 3 with elements strictly increasing from starting till an index i, and then strictly decreasing from index i to last index. More formally arr[0] < arr[1] < arr[i] >arr[i+1] > arr[i+2] > arr[N-1].
Examples
Input: arr[] = {4, 4, 3, 2, 1}
Output: falseInput: arr = {1, 2, 3, 4, 9, 8, 7, 6, 5}
Output: true
Approach: This problem can be solved by traversing the array in two parts. Follow the steps below to solve the given problem.
- First, traverse from start till the index where the current element becomes smaller than its immediate previous one.
- Then from this index, traverse till the last index and check if the current element is strictly smaller than the previous one.
- If both the conditions are true, return true.
- Else at any point, the condition fails, return false.
Below is the implementation of the above approach.
C++
// C++ program for above approach #include <bits/stdc++.h> using namespace std; // Function to check if the given array // is mountain array or not bool isMountainArray(vector< int >& arr) { if (arr.size() < 3) return false ; int flag = 0, i = 0; for (i = 1; i < arr.size(); i++) if (arr[i] <= arr[i - 1]) break ; if (i == arr.size() || i == 1) return false ; for (; i < arr.size(); i++) if (arr[i] >= arr[i - 1]) break ; return i == arr.size(); } // Driver Code int main() { vector< int > arr = { 1, 2, 3, 4, 9, 8, 7, 6, 5 }; cout << (isMountainArray(arr) ? "true" : "false" ); return 0; } |
Java
// Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG { // Function to check if the given array // is mountain array or not static Boolean isMountainArray( int arr[]) { if (arr.length < 3 ) return false ; int flag = 0 , i = 0 ; for (i = 1 ; i < arr.length; i++) if (arr[i] <= arr[i - 1 ]) break ; if (i == arr.length || i == 1 ) return false ; for (; i < arr.length; i++) if (arr[i] >= arr[i - 1 ]) break ; return i == arr.length; } // Driver Code public static void main (String[] args) { int arr[] = { 1 , 2 , 3 , 4 , 9 , 8 , 7 , 6 , 5 }; System.out.println(isMountainArray(arr) ? "true" : "false" ); } } // This code is contributed by hrithikgarg03188. |
Python3
# Python 3 program for above approach # Function to check if the given array # is mountain array or not def isMountainArray(arr): if ( len (arr) < 3 ): return False flag = 0 i = 0 for i in range ( 1 , len (arr)): if (arr[i] < = arr[i - 1 ]): break if (i = = len (arr) or i = = 1 ): return False while i < len (arr): if (arr[i] > = arr[i - 1 ]): break i + = 1 return i = = len (arr) # Driver Code if __name__ = = "__main__" : arr = [ 1 , 2 , 3 , 4 , 9 , 8 , 7 , 6 , 5 ] if (isMountainArray(arr)): print ( "true" ) else : print ( "false" ) # This code is contributed by ukasp. |
C#
// C# program for above approach using System; class GFG { // Function to check if the given array // is mountain array or not static bool isMountainArray( int [] arr) { if (arr.Length < 3) return false ; int i = 0; for (i = 1; i < arr.Length; i++) if (arr[i] <= arr[i - 1]) break ; if (i == arr.Length || i == 1) return false ; for (; i < arr.Length; i++) if (arr[i] >= arr[i - 1]) break ; return i == arr.Length; } // Driver Code public static int Main() { int [] arr = { 1, 2, 3, 4, 9, 8, 7, 6, 5 }; Console.Write(isMountainArray(arr) ? "true" : "false" ); return 0; } } // This code is contributed by Taranpreet |
Javascript
<script> // JavaScript program for above approach // Function to check if the given array // is mountain array or not const isMountainArray = (arr) => { if (arr.length < 3) return false ; let flag = 0, i = 0; for (i = 1; i < arr.length; i++) if (arr[i] <= arr[i - 1]) break ; if (i == arr.length || i == 1) return false ; for (; i < arr.length; i++) if (arr[i] >= arr[i - 1]) break ; return i == arr.length; } // Driver Code let arr = [1, 2, 3, 4, 9, 8, 7, 6, 5]; (isMountainArray(arr) ? document.write( "true" ) : document.write( "false" )); // This code is contributed by rakeshsahni </script> |
true
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!