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

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.

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

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 ` `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=A[j+1]){ ` `                 `  `            ``// adjusting nextStart; ` `            ``// this will be necessarily executed at least once, ` `            ``// when we detect the start of the descent ` `            ``if` `(jA[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); ` `    ``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=A[j+``1``]){ ` `                 `  `                ``// adjusting nextStart; ` `                ``// this will be necessarily executed at least once, ` `                ``// when we detect the start of the descent ` `                ``if` `(jA[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

 `= ``\$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. ` `?> `

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 contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.