Open In App

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

Last Updated : 12 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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) time 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.

Implementation:

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

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads