WAP to Validate Array Is a Mountain Array & Peak Index

Validate Array Is a Mountain Array & Peak Index: This Java program checks if the input array is a valid mountain array or not. A valid mountain array is an array that represents a mountain-like pattern, i.e., it first strictly increases, reaches a peak, and then strictly decreases.

Validate Given Array Is A Valid Mountain Array

package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class InterviewPrograms89 
{
	public static boolean validMountArray(int[] Arr) 
	{
		int i = 0;
		int j = Arr.length - 1;
		int n = Arr.length - 1;
		while (i + 1 < n && Arr[i] < Arr[i+1]) 
		{
			i++;
		}

		while (j > 0 && Arr[j] < Arr[j-1]) 
		{
			j--;
		}

		return (i > 0 && i == j && j < n);
	}
	public static void main(String[] args)
	{
		int num,i;

		// taking the inputs
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter the number of elements : ");
		num = sc.nextInt();
		int arr[]=new int[num];
		System.out.print ("Enter the array elements : ");
		for (i=0; i<num; i++ )
		{
			arr[i] = sc.nextInt();
		}

		System.out.println(validMountArray(arr));
	}
}

Output

Enter the number of elements : 3
Enter the array elements : 1
2
1
true

Alternative Way 1:

This Java program checks whether a subarray of the given array is in the form of a mountain or not. A subarray is considered to be in the form of a mountain if an element exists within the subarray such that all elements to its left are in strictly increasing order and all elements to its right are in strictly decreasing order.

package com.softwaretestingo.interviewprograms;
public class InterviewPrograms89_1 
{
	// Find Whether A Subarray Is In Form Of A Mountain Or Not
	static void buildArrays(int arr[], int N, int arrayL[], int arrayR[])
	{
		arrayL[0] = 0;
		int increasingNumber = 0;
		for (int i = 1; i < N; i++)
		{
			if (arr[i] > arr[i - 1])
				increasingNumber = i;
			arrayL[i] = increasingNumber;
		}
		arrayR[N - 1] = N - 1;
		int decreasingNumber = N - 1;
		for (int i = N - 2; i >= 0; i--)
		{
			if (arr[i] > arr[i + 1])
				decreasingNumber = i;
			arrayR[i] = decreasingNumber;
		}
	}

	static boolean solveQuery(int arr[], int arrayL[], int arrayR[], int Left, int Right)
	{
		return (arrayR[Left] >= arrayL[Right]);
	}
	public static void main(String[] args)
	{
		int arr[] = {3,4,5,6,1,5,1,2,1};
		int N = arr.length;
		int arrayL[] = new int[N];
		int arrayR[] = new int[N];
		buildArrays(arr, N, arrayL, arrayR);
		int L = 0;
		int R = 3;
		if (solveQuery(arr, arrayL, arrayR, L, R))
			System.out.println("Mountain form");
		else
			System.out.println("Not a mountain form");
		L = 5;
		R = 7;
		if (solveQuery(arr, arrayL, arrayR, L, R))
			System.out.println("Mountain form");
		else
			System.out.println("Not a mountain form");
	}
}

Output

Mountain form
Not a mountain form

Find the Peak Element Index Of the Mountain Array

This Java program is designed to find the index of the peak element in a given mountain array. A mountain array initially increases and then decreases. The peak element in a mountain array is the element that is greater than its adjacent elements.

package com.softwaretestingo.interviewprograms;
public class InterviewPrograms89_2 
{
	//Find the Peak Element Index Of the Mountain Array
	public static int getPeakIndex(int[] array) 
	{
		int low = 0;
		int high = array.length - 1;
		int mid;
		while (low<high) 
		{
			mid = low + (high - low) / 2;
			if (array[mid] >= array[mid + 1]) 
			{
				high = mid;
			} 
			else {
				low = mid + 1;
			}
		}
		return low;
	}

	public static void main(String[] args)
	{
		int mountainArray[] = { 4, 8, 16, 32, 27, 9, 3 };
		int peak = getPeakIndex(mountainArray);
		System.out.println("Peak index is:" + peak);
	}
}

Output

Peak index is:3

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment