Find whether a subarray is in form of a mountain or not

3.2

We are given an array of integers and a range, we need to find whether the subarray which falls in this range has values in form of a mountain or not. All values of the subarray are said to be in form of a mountain if either all values are increasing or decreasing or first increasing and then decreasing.
More formally a subarray [a1, a2, a3 … aN] is said to be in form of a mountain if there exist an integer K, 1 <= K <= N such that,
a1 <= a2 <= a3 .. <= aK >= a(K+1) >= a(K+2) …. >= aN
Examples:

Arr[]  = [2 3 2 4 4 6 3 2]
Range = [0, 2]
Output yes because [2 3 2] subarray first 
increases and then decreases

Range = [2, 7]
Output yes because [2 4 4 6 3 2] subarray 
first increases and then decreases

Range = [2, 3]
Output yes because [2 4] subarray increases

Range = [1, 3]
Output no because [3 2 4] is not in the form 
above stated

We can solve this problem by first some preprocessing then we can answer for each subarray in the constant amount of time. We maintain two arrays left and right where left[i] stores the last index on left side which is increasing i.e. greater than its previous element and right[i] will store the first index on the right side which is decreasing i.e. greater than its next element. Once we maintained these arrays we can answer each subarray in constant time. Suppose range [l, r] is given then only if right[l] >= left[r], the subarray will be in form of a mountain otherwise not because the first index in decreasing form (i.e. right[l]) should come later than last index in increasing form (i.e. left[r]).
This procedure is demonstrated for above example array.

Arr[] = [2 3 2 4 4 6 3 2]
Using above procedure building left and right array,
left[]   =  [0 1 1 3 3 5 5 5]
right[] =  [1 1 5 5 5 5 6 7]
Range = [2, 4]
Now right[2] is 5 and left[4] is 3 that means at
index 2 first decreasing element is right to the 
last increasing element at index 4, so they should
have a mountain form.

Range = [0, 3]
Now right[0] is 1 and left[3] is 3 that means at
index 0 first decreasing element is left to the 
last increasing element at index 3, so the subarray 
corresponding to this range does not have mountain 
form.  We can see this in the array itself, right[0]
is 1 which is value 3 and left[3] is 3 which is 
value 4 so 4 which is in increasing form (due to 
previous value 2) comes later to 3 which is in 
decreasing form (due to next value 2), mountain form
was not possible here, same information is carried 
out with the help of left and right array.

Auxiliary space for this solution is O(N) and preprocessing takes O(N) time, after that each subarray can be handled in constant time.

C/C++

// C++ program to check whether a subarray is in
// mountain form or not
#include <bits/stdc++.h>
using namespace std;

//	Utility method to construct left and right array
int preprocess(int arr[], int N, int left[], int right[])
{
    //	initialize first left index as that index only
    left[0] = 0;
    int lastIncr = 0;

    for (int i = 1; i < N; i++)
    {
        // if current value is greater than previous,
        // update last increasing
        if (arr[i] > arr[i - 1])
            lastIncr = i;
        left[i] = lastIncr;
    }

    //	initialize last right index as that index only
    right[N - 1] = N - 1;
    int firstDecr = N - 1;

    for (int i = N - 2; i >= 0; i--)
    {
        // if current value is greater than next,
        // update first decreasing
        if (arr[i] > arr[i + 1])
            firstDecr = i;
        right[i] = firstDecr;
    }
}

//	method returns true if arr[L..R] is in mountain form
bool isSubarrayMountainForm(int arr[], int left[],
                             int right[], int L, int R)
{
    // return true only if right at starting range is
    // greater than left at ending range
    return (right[L] >= left[R]);
}

//	Driver code to test above methods
int main()
{
    int arr[] = {2, 3, 2, 4, 4, 6, 3, 2};
    int N = sizeof(arr) / sizeof(int);

    int left[N], right[N];
    preprocess(arr, N, left, right);

    int L = 0;
    int R = 2;
    if (isSubarrayMountainForm(arr, left, right, L, R))
        cout << "Subarray is in mountain form\n";
    else
        cout << "Subarray is not in mountain form\n";

    L = 1;
    R = 3;
    if (isSubarrayMountainForm(arr, left, right, L, R))
        cout << "Subarray is in mountain form\n";
    else
        cout << "Subarray is not in mountain form\n";

    return 0;
}

Java

// Java program to check whether a subarray is in
// mountain form or not
class SubArray
{
    // Utility method to construct left and right array
    static void preprocess(int arr[], int N, int left[], int right[])
    {
    	// initialize first left index as that index only
    	left[0] = 0;
    	int lastIncr = 0;
    
    	for (int i = 1; i < N; i++)
    	{
        	// if current value is greater than previous,
        	// update last increasing
        	if (arr[i] > arr[i - 1])
        			lastIncr = i;
        	left[i] = lastIncr;
    	}
    
    	// initialize last right index as that index only
    	right[N - 1] = N - 1;
    	int firstDecr = N - 1;
    
    	for (int i = N - 2; i >= 0; i--)
    	{
        	// if current value is greater than next,
        	// update first decreasing
        	if (arr[i] > arr[i + 1])
        			firstDecr = i;
        	right[i] = firstDecr;
    	}
    }
    
    // method returns true if arr[L..R] is in mountain form
    static boolean isSubarrayMountainForm(int arr[], int left[],
    								int right[], int L, int R)
    {
    	// return true only if right at starting range is
    	// greater than left at ending range
    	return (right[L] >= left[R]);
    }
    
    public static void main(String[] args)
    {
    	int arr[] = {2, 3, 2, 4, 4, 6, 3, 2};
    	int N = arr.length;
    	int left[] = new int[N];
    	int right[] = new int[N];
    	preprocess(arr, N, left, right);
    	int L = 0;
    	int R = 2;
    	
    	if (isSubarrayMountainForm(arr, left, right, L, R))
    		System.out.println("Subarray is in mountain form");
    	else
    		System.out.println("Subarray is not in mountain form");
    
    	L = 1;
    	R = 3;
    
    	if (isSubarrayMountainForm(arr, left, right, L, R))
    		System.out.println("Subarray is in mountain form");
    	else
    		System.out.println("Subarray is not in mountain form");
    }
}
// This Code is Contributed by Saket Kumar


Output:

Subarray is in mountain form
Subarray is not in mountain form

This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



3.2 Average Difficulty : 3.2/5.0
Based on 11 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.