Skip to content
Related Articles

Related Articles

Improve Article

Maximum Length Bitonic Subarray | Set 2 (O(n) time and O(1) Space)

  • Difficulty Level : Hard
  • Last Updated : 23 Jun, 2021

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.
 

C++




// C++ program to find length of longest bitonic
// subarray. O(n) time and O(1) extra space
#include <iostream>
using namespace std;
 
// Function to find length of longest bitonic
// subarray
int bitonic(int *A, int n)
{
    // if A is empty
    if (n == 0)
        return 0;
         
    // initializing max_len
    int maxLen=1;
         
    int start=0;
    int nextStart=0;
         
    int j =0;
    while (j < n-1)
    {
        // look for end of ascent      
        while (j<n-1 && A[j]<=A[j+1])
            j++;
             
        // look for end of descent      
        while (j<n-1 && A[j]>=A[j+1]){
                 
            // adjusting nextStart;
            // this will be necessarily executed at least once,
            // when we detect the start of the descent
            if (j<n-1 && A[j]>A[j+1])
                nextStart=j+1;
                 
            j++;
        }
             
        // updating maxLen, if required
        maxLen = max(maxLen,j-(start-1));
             
        start=nextStart;
    }
         
    return maxLen;
}
 
int main()
{
    int A[] = {12, 4, 78, 90, 45, 23};
    int n = sizeof(A)/sizeof(A[0]);
    printf("Length of max length Bitonic "
            "Subarray is %d", bitonic(A, n));
    return 0;
}

Java




// Java program to find length of longest bitonic
// subarray O(n) time and O(1) extra space
 
public class MaxLengthBitonic
{
    // Method to find length of longest bitonic
    // subarray
    static int maxLenBitonic(int[] A,int n)
    {
        // if A is empty
        if (n == 0)
            return 0;
         
        // initializing max_len
        int maxLen=1;
         
        int start=0;
        int nextStart=0;
         
        int j =0;
        while (j < n-1)
        {
            // look for end of ascent      
            while (j<n-1 && A[j]<=A[j+1])
                j++;
             
            // look for end of descent      
            while (j<n-1 && A[j]>=A[j+1]){
                 
                // adjusting nextStart;
                // this will be necessarily executed at least once,
                // when we detect the start of the descent
                if (j<n-1 && A[j]>A[j+1])
                    nextStart=j+1;
                 
                j++;
            }
             
            // updating maxLen, if required
            maxLen = Math.max(maxLen,j-(start-1));
             
            start=nextStart;
        }
         
        return maxLen;
    }
     
    public static void  main(String[] args)
    {
        int A[] = {12, 4, 78, 90, 45, 23};
        System.out.println("Length of maximal length bitonic " +
                            "subarray is " + maxLenBitonic(A,A.length));
 
    }
}
// This code is contributed by Markus Schott

Python3




# Python3 program to find length of longest bitonic
# subarray. O(n) time and O(1) extra space
 
# Function to find length of longest
# bitonic subarray
def bitonic(A, n):
 
    # if A is empty
    if (n == 0):
        return 0;
         
    # initializing max_len
    maxLen = 1;
         
    start = 0;
    nextStart = 0;
         
    j = 0;
    while (j < n - 1):
     
        # look for end of ascent    
        while (j < n - 1 and A[j] <= A[j + 1]):
            j = j + 1;
             
        # look for end of descent
        while (j < n - 1 and A[j] >= A[j + 1]):
                 
            # adjusting nextStart;
            # this will be necessarily executed
            # at least once, when we detect the
            # start of the descent
            if (j < n - 1 and A[j] > A[j + 1]):
                nextStart = j + 1;
                 
            j = j + 1;
         
        # updating maxLen, if required
        maxLen = max(maxLen, j - (start - 1));
             
        start = nextStart;
     
    return maxLen;
 
# Driver Code
A = [12, 4, 78, 90, 45, 23];
n = len(A);
print("Length of max length Bitonic Subarray is",
                                  bitonic(A, n));
 
# This code is contributed by Shivi_Aggarwal

C#




// C# program to find length of longest bitonic
// subarray O(n) time and O(1) extra space
using System;
 
class MaxLengthBitonic
{
    // Method to find length of
    // longest bitonic subarray
    static int maxLenBitonic(int[] A, int n)
    {
        // if A is empty
        if (n == 0)
            return 0;
         
        // initializing max_len
        int maxLen = 1;
         
        int start = 0;
        int nextStart = 0;
         
        int j = 0;
        while (j < n-1)
        {
            // look for end of ascent    
            while (j < n-1 && A[j] <= A[j+1])
                j++;
             
            // look for end of descent    
            while (j < n-1 && A[j] >= A[j+1]){
                 
                // adjusting nextStart;
                // this will be necessarily executed at least once,
                // when we detect the start of the descent
                if (j < n-1 && A[j] > A[j+1])
                    nextStart=j + 1;
                 
                j++;
            }
             
            // updating maxLen, if required
            maxLen = Math.Max(maxLen, j - (start - 1));
             
            start=nextStart;
        }
        return maxLen;
    }
     
    public static void Main()
    {
        int []A = {12, 4, 78, 90, 45, 23};
        Console.Write("Length of maximal length bitonic " +
                      "subarray is " + maxLenBitonic(A, A.Length));
    }
}
 
// This code is contributed by nitin mittal.

PHP




<?php
// PHP program to find length of
// longest bitonic subarray.
// O(n) time and O(1) extra space
 
// Function to find length of
// longest bitonic subarray
function bitonic($A, $n)
{
     
    // if A is empty
    if ($n == 0)
        return 0;
         
    // initializing max_len
    $maxLen = 1;
         
    $start = 0;
    $nextStart = 0;
         
    $j = 0;
    while ($j < $n - 1)
    {
         
        // look for end of ascent    
        while ($j < $n - 1 &&
               $A[$j] <= $A[$j + 1])
            $j++;
             
        // look for end of descent    
        while ($j < $n - 1 &&
               $A[$j] >= $A[$j + 1])
        {
                 
            // adjusting nextStart;
            // this will be necessarily
            // executed at least once,
            // when we detect the start
            // of the descent
            if ($j < $n - 1 && $A[$j] >
                          $A[$j + 1])
                $nextStart = $j + 1;
            $j++;
        }
             
        // updating maxLen,
        // if required
        $maxLen = max($maxLen, $j - ($start - 1));
        $start = $nextStart;
    }
         
    return $maxLen;
}
 
    // Driver Code
    $A = array(12, 4, 78, 90, 45, 23);
    $n = sizeof($A);
    echo "Length of max length Bitonic "
        ,"Subarray is ", bitonic($A, $n);
 
// This code is contributed by nitin mittal.
?>

Javascript




<script>
 
// JavaScript program to find length of
// longest bitonic subarray.
// O(n) time and O(1) extra space
 
// Function to find length of
// longest bitonic subarray
function bitonic(A, n) {
 
    // if A is empty
    if (n == 0)
        return 0;
 
    // initializing max_len
    let maxLen = 1;
 
    let start = 0;
    let nextStart = 0;
 
    let j = 0;
    while (j < n - 1) {
 
        // look for end of ascent   
        while (j < n - 1 &&
            A[j] <= A[j + 1])
            j++;
 
        // look for end of descent   
        while (j < n - 1 &&
            A[j] >= A[j + 1]) {
 
            // adjusting nextStart;
            // this will be necessarily
            // executed at least once,
            // when we detect the start
            // of the descent
            if (j < n - 1 && A[j] >
                A[j + 1])
                nextStart = j + 1;
            j++;
        }
 
        // updating maxLen,
        // if required
        maxLen = Math.max(maxLen, j - (start - 1));
        start = nextStart;
    }
 
    return maxLen;
}
 
// Driver Code
let A = new Array(12, 4, 78, 90, 45, 23);
let n = A.length;
document.write("Length of max length Bitonic "
    + "Subarray is " + bitonic(A, n));
 
// This code is contributed by gfgking
 
</script>

Output:  

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 write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :