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
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 is 5 and left 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 is 1 and left 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 is 1 which is value 3 and left 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.
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 email@example.com. 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.
- Find the subarray with least average
- Find mean of subarray means in a given array
- Find the length of largest subarray with 0 sum
- Find subarray with given sum | Set 1 (Nonnegative Numbers)
- Find the maximum subarray XOR in a given array
- Find Maximum Sum Strictly Increasing Subarray
- Find smallest subarray that contains all elements in same order
- Find maximum average subarray of k length
- Find maximum (or minimum) sum of a subarray of size k
- Find subarray with given sum | Set 2 (Handles Negative Numbers)
- Find the longest Fibonacci-like subarray of the given array
- Find an element in an array such that elements form a strictly decreasing and increasing sequence
- Maximize the subarray sum after multiplying all elements of any subarray with X
- Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted
- Maximum length of subarray such that sum of the subarray is even
Improved By : jit_t