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 notbool 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 Codeint 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 approachimport java.io.*;import java.lang.*;import java.util.*;Â
class GFG {Â
// Function to check if the given array// is mountain array or notstatic 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 notdef 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 Codeif __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 approachusing 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!
