Given an array arr with N elements, the task is to find out the longest sub-array which has the shape of a mountain.
A mountain sub-array consists of elements that are initially in ascending order until a peak element is reached and beyond the peak element all other elements of the sub-array are in decreasing order.
Input: arr = [2, 2, 2]
No sub-array exists that shows the behavior of a mountain sub-array.
Input: arr = [1, 3, 1, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5]
There are two sub-arrays that can be considered as mountain sub-arrays. The first one is from index 0 – 2 (3 elements) and next one is from index 2 – 12 (11 elements). As 11 > 2, our answer is 11.
Go through every possible sub-array and check whether it is a mountain sub-array or not. This might take a long time to find the solution and the time complexity for the above approach can be estimated as O(N*N) to go through every possible sub-array and O(N) to check whether it is a mountain sub-array or not. Thus, the overall time complexity for the program is O(N3) which is very high.
- If the length of the given array is less than 3, print 0 as it is not possible to have a mountain sub-array in such a case.
- Set the maximum length to 0 initially.
- Use the two-pointer technique (‘begin’ pointer and ‘end’ pointer) to find out the longest mountain sub-array in the given array.
- When an increasing sub-array is encountered, mark the beginning index of that increasing sub-array in the ‘begin’ pointer.
- If an index value is found in the ‘end’ pointer then reset the values in both the pointers as it marks the beginning of a new mountain sub-array.
- When a decreasing sub-array us encountered, mark the ending index of the mountain sub-array in the ‘end’ pointer.
- Calculate the length of the current mountain sub-array, compare it with the current maximum length of all-mountain sub-arrays traversed until now and keep updating the current maximum length.
Below is the implementation of the above described efficient approach:
Time Complexity: O(N)
Auxiliary Space Complexity: O(1)
- Find whether a subarray is in form of a mountain or not
- Maximize the subarray sum after multiplying all elements of any subarray with X
- Maximum length of subarray such that sum of the subarray is even
- Count of subarray that does not contain any subarray with sum 0
- Maximum length of subarray such that all elements are equal in the subarray
- First subarray having sum at least half the maximum sum of any subarray of size K
- Smallest pair of indices with product of subarray co-prime with product of the subarray on the left or right
- Longest Subarray of non-negative Integers
- Longest Prime Subarray after removing one element
- Longest Subarray having sum of elements atmost 'k'
- Longest increasing subarray
- Longest subarray having count of 1s one more than count of 0s
- Longest alternating (positive and negative) subarray starting at every index
- Longest subarray not having more than K distinct elements
- Length of longest increasing circular subarray
- Longest subarray with sum divisible by k
- Longest subarray with only one value greater than k
- Longest subarray such that adjacent elements have at least one common digit | Set 1
- Longest subarray having average greater than or equal to x
- Longest Subarray with first element greater than or equal to Last element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.