Maximum Length Bitonic Subarray | Set 2 (O(n) time and O(1) Space)
Last Updated :
12 Jul, 2022
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++
#include <iostream>
using namespace std;
int bitonic( int *A, int n)
{
if (n == 0)
return 0;
int maxLen=1;
int start=0;
int nextStart=0;
int j =0;
while (j < n-1)
{
while (j<n-1 && A[j]<=A[j+1])
j++;
while (j<n-1 && A[j]>=A[j+1]){
if (j<n-1 && A[j]>A[j+1])
nextStart=j+1;
j++;
}
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
public class MaxLengthBitonic
{
static int maxLenBitonic( int [] A, int n)
{
if (n == 0 )
return 0 ;
int maxLen= 1 ;
int start= 0 ;
int nextStart= 0 ;
int j = 0 ;
while (j < n- 1 )
{
while (j<n- 1 && A[j]<=A[j+ 1 ])
j++;
while (j<n- 1 && A[j]>=A[j+ 1 ]){
if (j<n- 1 && A[j]>A[j+ 1 ])
nextStart=j+ 1 ;
j++;
}
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));
}
}
|
Python3
def bitonic(A, n):
if (n = = 0 ):
return 0 ;
maxLen = 1 ;
start = 0 ;
nextStart = 0 ;
j = 0 ;
while (j < n - 1 ):
while (j < n - 1 and A[j] < = A[j + 1 ]):
j = j + 1 ;
while (j < n - 1 and A[j] > = A[j + 1 ]):
if (j < n - 1 and A[j] > A[j + 1 ]):
nextStart = j + 1 ;
j = j + 1 ;
maxLen = max (maxLen, j - (start - 1 ));
start = nextStart;
return maxLen;
A = [ 12 , 4 , 78 , 90 , 45 , 23 ];
n = len (A);
print ( "Length of max length Bitonic Subarray is" ,
bitonic(A, n));
|
C#
using System;
class MaxLengthBitonic
{
static int maxLenBitonic( int [] A, int n)
{
if (n == 0)
return 0;
int maxLen = 1;
int start = 0;
int nextStart = 0;
int j = 0;
while (j < n-1)
{
while (j < n-1 && A[j] <= A[j+1])
j++;
while (j < n-1 && A[j] >= A[j+1]){
if (j < n-1 && A[j] > A[j+1])
nextStart=j + 1;
j++;
}
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));
}
}
|
PHP
<?php
function bitonic( $A , $n )
{
if ( $n == 0)
return 0;
$maxLen = 1;
$start = 0;
$nextStart = 0;
$j = 0;
while ( $j < $n - 1)
{
while ( $j < $n - 1 &&
$A [ $j ] <= $A [ $j + 1])
$j ++;
while ( $j < $n - 1 &&
$A [ $j ] >= $A [ $j + 1])
{
if ( $j < $n - 1 && $A [ $j ] >
$A [ $j + 1])
$nextStart = $j + 1;
$j ++;
}
$maxLen = max( $maxLen , $j - ( $start - 1));
$start = $nextStart ;
}
return $maxLen ;
}
$A = array (12, 4, 78, 90, 45, 23);
$n = sizeof( $A );
echo "Length of max length Bitonic "
, "Subarray is " , bitonic( $A , $n );
?>
|
Javascript
<script>
function bitonic(A, n) {
if (n == 0)
return 0;
let maxLen = 1;
let start = 0;
let nextStart = 0;
let j = 0;
while (j < n - 1) {
while (j < n - 1 &&
A[j] <= A[j + 1])
j++;
while (j < n - 1 &&
A[j] >= A[j + 1]) {
if (j < n - 1 && A[j] >
A[j + 1])
nextStart = j + 1;
j++;
}
maxLen = Math.max(maxLen, j - (start - 1));
start = nextStart;
}
return maxLen;
}
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));
</script>
|
Output
Length of max length Bitonic Subarray is 5
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...