703130. Maximum length Bitonic Subarray

Medium

Here is a detailed Markdown blog post for the GeeksforGeeks problem "Maximum length Bitonic Subarray":

Maximum length Bitonic Subarray

Summary

Given an array, we need to find the maximum length bitonic subarray. A bitonic sequence is a sequence that first increases and then decreases. This problem requires us to identify the longest such subsequence in the given array.

Detailed Explanation

To solve this problem, we can start by identifying all possible bitonic subsequences in the given array. We can do this by iterating through the array and marking each element as either increasing or decreasing based on its position relative to the previous elements.

Once we have identified all the bitonic subsequences, we need to find the longest one among them. This can be done by comparing the lengths of all the bitonic subsequences found so far and returning the length of the longest one.

Here is a step-by-step breakdown of the solution:

  1. Initialize an empty array bitonic_subarrays to store all possible bitonic subsequences.
  2. Iterate through the given array from left to right, marking each element as either increasing or decreasing based on its position relative to the previous elements. For example, if the current element is greater than the previous one, mark it as increasing; otherwise, mark it as decreasing.
  3. Whenever you encounter an element that changes from increasing to decreasing (or vice versa), it means you have found a new bitonic subarray. Store this subarray in bitonic_subarrays.
  4. Once you have processed the entire array, iterate through bitonic_subarrays and find the longest one by comparing their lengths.
  5. Return the length of the longest bitonic subarray found.

Here is an ASCII art diagram to illustrate the above steps:

 0 1 2 3 4 5 6
I I D D D D I
----------------
| Increasing | Decreasing |
|-----------|
| Bitonic Subarray | Bitonic Subarray |
|-----------|

Time complexity: O(n), where n is the length of the given array.

Space complexity: O(n), as we need to store all possible bitonic subsequences in bitonic_subarrays.

Optimized Solutions

Java

View on GeeksforGeeks
public int maximumLengthBitonicSubarray(int[] arr) {
    int max_length = 0;
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] > arr[i - 1]) {
            // Found an increasing subarray, check if it's bitonic
            int length = 1;
            while (i + length < arr.length && arr[i + length] > arr[i + length - 1]) {
                length++;
            }
            // Check if the subarray is decreasing after the increase
            int decreasing_length = 0;
            while (i + length + decreasing_length < arr.length && arr[i + length + decreasing_length] < arr[i + length + decreasing_length - 1]) {
                decreasing_length++;
            }
            max_length = Math.max(max_length, length + decreasing_length);
        }
    }
    return max_length;
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.