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 the form of a mountain or not. All values of the subarray are said to be in the 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
Input : Arr = [2 3 2 4 4 6 3 2], Range = [0, 2] Output : Yes Explanation: The output is yes , subarray is [2 3 2], so subarray first increases and then decreases Input: Arr = [2 3 2 4 4 6 3 2], Range = [2, 7] Output: Yes Explanation: The output is yes , subarray is [2 4 4 6 3 2], so subarray first increases and then decreases Input: Arr= [2 3 2 4 4 6 3 2], Range = [1, 3] Output: no Explanation: The output is no, subarray is [3 2 4], so subarray is not in the form above stated
- Approach:The problem has multiple queries so for each query the solution should be calculated with least possible time complexity. So create two extra spaces of the length of the original array. For every element find the last index on the left side which is increasing i.e. greater than its previous element and find the element on the right side will store the first index on the right side which is decreasing i.e. greater than its next element. If these value can be calculated for every index in constant time then for every given range the answer can be given in constant time.
- Create two extra spaces of length n,left and right and a extra variable lastptr
- Initilize left = 0 and lastptr = 0
- Traverse the original array from second index to the end
- For every index check if it is greater than the pervious element, if yes then update the lastptr with the current index.
- For every index store the lastptr in left[i]
- initilize right[N-1] = N-1 and lastptr = N-1
- Traverse the original array from second last index to the start
- For every index check if it is greater than the next element, if yes then update the lastptr with the current index.
- For every index store the lastptr in right[i]
- Now process the queries
- for every query l, r, if right[l] >= left[r] then print yes else no
Subarray is in mountain form Subarray is not in mountain form
- Complexity Analysis:
- Time Complexiity:O(n).
Only two traversals are needed so the time complexity is O(n).
- Space Complexity:O(n).
Two extra space of length n is required so the space complexity is O(n).
- Time Complexiity:O(n).
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.