Find whether given Array is in form of a mountain or not
Last Updated :
28 Mar, 2022
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: false
Input: 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++
#include <bits/stdc++.h>
using namespace std;
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();
}
int main()
{
vector< int > arr
= { 1, 2, 3, 4, 9,
8, 7, 6, 5 };
cout << (isMountainArray(arr)
? "true"
: "false" );
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
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;
}
public static void main (String[] args) {
int arr[] = { 1 , 2 , 3 , 4 , 9 ,
8 , 7 , 6 , 5 };
System.out.println(isMountainArray(arr)
? "true"
: "false" );
}
}
|
Python3
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)
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 , 9 ,
8 , 7 , 6 , 5 ]
if (isMountainArray(arr)):
print ( "true" )
else :
print ( "false" )
|
C#
using System;
class GFG
{
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;
}
public static int Main()
{
int [] arr = { 1, 2, 3, 4, 9, 8, 7, 6, 5 };
Console.Write(isMountainArray(arr) ? "true"
: "false" );
return 0;
}
}
|
Javascript
<script>
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;
}
let arr = [1, 2, 3, 4, 9,
8, 7, 6, 5];
(isMountainArray(arr)
? document.write( "true" )
: document.write( "false" ));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...