Given an array A[0 … n-1] containing n positive integers, a subarray A[i … j] is bitonic if there is a k with i <= k <= j such that A[i] = .. A[j – 1] > = A[j]. Write a function that takes an array as argument and returns the length of the maximum length bitonic subarray.
We have discussed O(n) time and O(n) space approach in below post.
Maximum Length Bitonic Subarray | Set 1 (O(n) tine and O(n) space)
In this set, we will discuss solution taking constant extra space.
The idea is to check longest bitonic subarray starting at A[i]. From A[i], first we will check for end of ascent and then end of descent.Overlapping of bitonic subarrays is taken into account by recording a nextStart position when it finds two equal values when going down the slope of the current subarray. If length of this subarray is greater than max_len, we will update max_len. We continue this process till end of array is reached.
Length of max length bitonic subarray is 5
This article is contributed by Gaurav Miglani. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Largest Sum Contiguous Subarray
- Maximum sum such that no two elements are adjacent
- Maximum and minimum of an array using minimum number of comparisons
- Maximum difference between two elements such that larger element appears after the smaller number
- Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted
- Find duplicates in O(n) time and O(1) extra space | Set 1
- Rearrange positive and negative numbers in O(n) time and O(1) extra space
- Sliding Window Maximum (Maximum of all subarrays of size k)
- Given an array arr, find the maximum j - i such that arr[j] > arr[i]
- Maximum Length Bitonic Subarray | Set 1 (O(n) time and O(n) space)
- Find the maximum element in an array which is first increasing and then decreasing
- Maximum Sum Increasing Subsequence | DP-14
- Find subarray with given sum | Set 1 (Nonnegative Numbers)
- Find a sorted subsequence of size 3 in linear time
- Largest subarray with equal number of 0s and 1s