Open In App

Maximum Length Bitonic Subarray

Last Updated : 25 Mar, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Given an array arr[0 ... n-1] of n positive integers, a subarray arr[i ... j] is bitonic if there exists an index k (i ≤ k ≤ j) such that arr[i] ≤ arr[i+1] ≤ ... ≤ arr[k] and arr[k] ≥ arr[k+1] ≥ ... ≥ arr[j].Find the length of the longest bitonic subarray.

Examples

Input: arr[] = [12, 4, 78, 90, 45, 23]
Output: 5
Explanation: The longest bitonic subarray is [4, 78, 90, 45, 23]. It starts increasing at 4, peaks at 90, and decreases to 23, giving a length of 5.

Input: arr[] = [10, 20, 30, 40] 
Output: 4
Explanation: The array [10, 20, 30, 40] is strictly increasing with no decreasing part, so the longest bitonic subarray is the entire array itself, giving a length of 4.

[Naive Approach] Using Nested Loops – O(n^3) time and O(1) space

We check all possible subarrays, trying each index as a peak. If a subarray follows the bitonic pattern, we update the longest length found.

C++
#include <bits/stdc++.h>
using namespace std;

// Check if subarray arr[s...e] is bitonic
bool isBit(const vector<int>& arr, int s, int e) {
    
    // Try each index as potential peak
    for (int p = s; p <= e; p++) { 
        bool nd = true, ni = true;
        
        // Check non-decreasing from s to p
        for (int i = s + 1; i <= p; i++) {
            if (arr[i] < arr[i - 1]) { nd = false; break; }
        }
        
        // Check non-increasing from p to e
        for (int i = p + 1; i <= e; i++) {
            if (arr[i] > arr[i - 1]) { ni = false; break; }
        }
        if (nd && ni)
            return true;
    }
    return false;
}

// Find the length of the longest bitonic subarray
int findLB(const vector<int>& arr) {
    int n = arr.size(), maxL = 0;
    // Enumerate all subarrays arr[i...j]
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            if (isBit(arr, i, j))
                maxL = max(maxL, j - i + 1);
        }
    }
    return maxL;
}

int main() {
    vector<int> arr = {12, 4, 78, 90, 45, 23};
    cout << findLB(arr) << endl;
    return 0;
}
Java Python C# JavaScript

Output
5

[Expected Approach] Using Peak-Based Mountain – O(n) time and O(n) space

For solving this problem we can imagine each number as the peak of a mountain. For every element, count how many consecutive numbers before it are rising (or flat) and how many after it are falling (or flat). Add these two counts and subtract one (to avoid counting the peak twice). The longest mountain found this way is answer.

C++
 #include <bits/stdc++.h>
using namespace std;

int bitonic(int arr[], int n) 
{ 
    // Length of increasing subarray
    // ending at all indexes 
    int inc[n]; 
    
    // Length of decreasing subarray 
    // starting at all indexes 
    int dec[n]; 
    int i, max; 

    // length of increasing sequence 
    // ending at first index is 1 
    inc[0] = 1; 

    // length of increasing sequence
    // starting at first index is 1 
    dec[n-1] = 1; 

    // Step 1) Construct increasing sequence array 
    for (i = 1; i < n; i++) 
    inc[i] = (arr[i] >= arr[i-1])? inc[i-1] + 1: 1; 

    // Step 2) Construct decreasing sequence array 
    for (i = n-2; i >= 0; i--) 
    dec[i] = (arr[i] >= arr[i+1])? dec[i+1] + 1: 1; 

    // Step 3) Find the length of
    // maximum length bitonic sequence 
    max = inc[0] + dec[0] - 1; 
    for (i = 1; i < n; i++) 
        if (inc[i] + dec[i] - 1 > max) 
            max = inc[i] + dec[i] - 1; 

    return max; 
} 

int main() 
{ 
    int arr[] = {12, 4, 78, 90, 45, 23}; 
    int n = sizeof(arr)/sizeof(arr[0]); 
    cout <<bitonic(arr, n); 
    return 0; 
} 
C Java Python C# JavaScript

Output
5

Time Complexity : O(n) 
Auxiliary Space : O(n)

[Optimized Approach] Single Traversal – O(n) time and O(1) space

The idea is to check longest bitonic subarray starting at arr[i]. From arr[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. Please refer Maximum Length Bitonic Subarray | Set 2 (O(n) time and O(1) Space) for implementation.



Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg