Open In App

Check for Mountain Array

Last Updated : 05 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

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.
C++
#include <iostream>
#include <vector>
using namespace std;

bool isMountain(vector<int> &arr)
{
    if (arr.size() < 3)
        return false;

    int i = 0;

    // Step 1: Traverse to find the peak, where the array stops increasing
    for (i = 1; i < arr.size(); i++)
        if (arr[i] <= arr[i - 1])
            break;

    // Step 2: Check if peak is at the start or the end of the array
    if (i == arr.size() || i == 1)
        return false;

    // Step 3: Check strictly decreasing part after the peak
    for (; i < arr.size(); i++)
        if (arr[i] >= arr[i - 1])
            break;

    // Step 4: If we reached the end, it's a mountain array
    return i == arr.size();
}

int main()
{
    vector<int> arr = {1, 2, 3, 4, 9, 8, 7, 6, 5};
    cout << (isMountain(arr) ? "true" : "false");
    return 0;
}
Java
import java.util.Arrays;

public class GfG{
    public static boolean isMountain(int[] arr) {
        if (arr.length < 3)
            return false;

        int i = 0;

        // Step 1: Traverse to find the peak, where the array stops increasing
        for (i = 1; i < arr.length; i++)
            if (arr[i] <= arr[i - 1])
                break;

        // Step 2: Check if peak is at the start or the end of the array
        if (i == arr.length || i == 1)
            return false;

        // Step 3: Check strictly decreasing part after the peak
        for (; i < arr.length; i++)
            if (arr[i] >= arr[i - 1])
                break;

        // Step 4: If we reached the end, it's a mountain array
        return i == arr.length;
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 9, 8, 7, 6, 5};
        System.out.println(isMountain(arr) ? "true" : "false");
    }
}
Python
def is_mountain(arr):
    if len(arr) < 3:
        return False

    i = 0

    # Step 1: Traverse to find the peak, where the array stops increasing
    for i in range(1, len(arr)):
        if arr[i] <= arr[i - 1]:
            break

    # Step 2: Check if peak is at the start or the end of the array
    if i == len(arr) or i == 1:
        return False

    # Step 3: Check strictly decreasing part after the peak
    for j in range(i, len(arr)):
        if arr[j] >= arr[j - 1]:
            break
    else:
        # If the loop completes successfully, this means it's a mountain array
        return True

    # Step 4: If we reached the end, it's a mountain array
    return j == len(arr)

if __name__ == '__main__':
    arr = [1, 2, 3, 4, 9, 8, 7, 6, 5]
    print('true' if is_mountain(arr) else 'false')
C#
using System;

class Program {
    public static bool IsMountain(int[] arr) {
        if (arr.Length < 3)
            return false;

        int i = 0;

        // Step 1: Traverse to find the peak, where the array stops increasing
        for (i = 1; i < arr.Length; i++)
            if (arr[i] <= arr[i - 1])
                break;

        // Step 2: Check if peak is at the start or the end of the array
        if (i == arr.Length || i == 1)
            return false;

        // Step 3: Check strictly decreasing part after the peak
        for (; i < arr.Length; i++)
            if (arr[i] >= arr[i - 1])
                break;

        // Step 4: If we reached the end, it's a mountain array
        return i == arr.Length;
    }

    static void Main() {
        int[] arr = new int[] { 1, 2, 3, 4, 9, 8, 7, 6, 5 };
        Console.WriteLine(IsMountain(arr) ? "true" : "false");
    }
}
JavaScript
function isMountain(arr) {
    if (arr.length < 3)
        return false;

    let i = 0;

    // Step 1: Traverse to find the peak, where the array stops increasing
    for (i = 1; i < arr.length; i++)
        if (arr[i] <= arr[i - 1])
            break;

    // Step 2: Check if peak is at the start or the end of the array
    if (i === arr.length || i === 1)
        return false;

    // Step 3: Check strictly decreasing part after the peak
    for (; i < arr.length; i++)
        if (arr[i] >= arr[i - 1])
            break;

    // Step 4: If we reached the end, it's a mountain array
    return i === arr.length;
}

const arr = [1, 2, 3, 4, 9, 8, 7, 6, 5];
console.log(isMountain(arr) ? "true" : "false");

Output
true

Time Complexity: O(n) 
Auxiliary Space: O(1) 


Article Tags :
Practice Tags :

Similar Reads