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.
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.
- Maximum Length Bitonic Subarray | Set 1 (O(n) time and O(n) space)
- Find bitonic point in given bitonic sequence
- Maximum sum bitonic subarray
- Print Longest Bitonic subsequence (Space Optimized Approach)
- Maximum length of subarray such that sum of the subarray is even
- Maximum length of subarray such that all elements are equal in the subarray
- Length of longest subarray of length at least 2 with maximum GCD
- Length of longest strict bitonic subsequence
- Find the maximum repeating number in O(n) time and O(1) extra space
- Maximum product of bitonic subsequence of size 3
- Find duplicates in O(n) time and O(1) extra space | Set 1
- Duplicates in an array in O(n) time and by using O(1) extra space | Set-3
- First subarray having sum at least half the maximum sum of any subarray of size K
- Rearrange positive and negative numbers in O(n) time and O(1) extra space
- Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers)
- Subarray with difference between maximum and minimum element greater than or equal to its length
- Generate a Bitonic array starting with N and adjacent difference of K
- Find subarray with given sum with negatives allowed in constant space
- Count frequencies of all elements in array in O(1) extra space and O(n) time
- Print left rotation of array in O(n) time and O(1) space