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

12 Jan, 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[i + 1] ... = A[k + 1] >= .. A[j – 1] > = A[j]. Write a function that takes an array as argument and returns the length of the maximum length bitonic subarray. Expected time complexity of the solution is O(n)Simple Examples 1) A[] = {12, 4, 78, 90, 45, 23}, the maximum length bitonic subarray is {4, 78, 90, 45, 23} which is of length 5.2) A[] = {20, 4, 1, 2, 3, 4, 2, 10}, the maximum length bitonic subarray is {1, 2, 3, 4, 2} which is of length 5.Extreme Examples 1) A[] = {10}, the single element is bitonic, so output is 1.2) A[] = {10, 20, 30, 40}, the complete array itself is bitonic, so output is 4.3) A[] = {40, 30, 20, 10}, the complete array itself is bitonic, so output is 4. 
Solution Let us consider the array {12, 4, 78, 90, 45, 23} to understand the solution. 1) Construct an auxiliary array inc[] from left to right such that inc[i] contains length of the nondecreasing subarray ending at arr[i]. For A[] = {12, 4, 78, 90, 45, 23}, inc[] is {1, 1, 2, 3, 1, 1} 2) Construct another array dec[] from right to left such that dec[i] contains length of nonincreasing subarray starting at arr[i]. For A[] = {12, 4, 78, 90, 45, 23}, dec[] is {2, 1, 1, 3, 2, 1}.3) Once we have the inc[] and dec[] arrays, all we need to do is find the maximum value of (inc[i] + dec[i] – 1). For {12, 4, 78, 90, 45, 23}, the max value of (inc[i] + dec[i] – 1) is 5 for i = 3.  
C++
C
Java
Python3
C#
PHP
Javascript
// C++ program to find length of// the longest bitonic subarray#include <bits/stdc++.h>using namespace std; int bitonic(int arr[], int n){    // Length of increasing subarray    // ending at all indexes    int inc[n];         // Length of decreasing subarray    // starting at all indexes    int dec[n];    int i, max;     // length of increasing sequence    // ending at first index is 1    inc[0] = 1;     // length of increasing sequence    // starting at first index is 1    dec[n-1] = 1;     // Step 1) Construct increasing sequence array    for (i = 1; i < n; i++)    inc[i] = (arr[i] >= arr[i-1])? inc[i-1] + 1: 1;     // Step 2) Construct decreasing sequence array    for (i = n-2; i >= 0; i--)    dec[i] = (arr[i] >= arr[i+1])? dec[i+1] + 1: 1;     // Step 3) Find the length of    // maximum length bitonic sequence    max = inc[0] + dec[0] - 1;    for (i = 1; i < n; i++)        if (inc[i] + dec[i] - 1 > max)            max = inc[i] + dec[i] - 1;     return max;} /* Driver code */int main(){    int arr[] = {12, 4, 78, 90, 45, 23};    int n = sizeof(arr)/sizeof(arr[0]);    cout << "nLength of max length Bitonic Subarray is " << bitonic(arr, n);    return 0;} // This is code is contributed by rathbhupendra
// C program to find length of the longest bitonic subarray#include<stdio.h>#include<stdlib.h> int bitonic(int arr[], int n){    int inc[n]; // Length of increasing subarray ending at all indexes    int dec[n]; // Length of decreasing subarray starting at all indexes    int i, max;     // length of increasing sequence ending at first index is 1    inc[0] = 1;     // length of increasing sequence starting at first index is 1    dec[n-1] = 1;     // Step 1) Construct increasing sequence array    for (i = 1; i < n; i++)       inc[i] = (arr[i] >= arr[i-1])? inc[i-1] + 1: 1;     // Step 2) Construct decreasing sequence array    for (i = n-2; i >= 0; i--)       dec[i] = (arr[i] >= arr[i+1])? dec[i+1] + 1: 1;     // Step 3) Find the length of maximum length bitonic sequence    max = inc[0] + dec[0] - 1;    for (i = 1; i < n; i++)        if (inc[i] + dec[i] - 1 > max)            max = inc[i] + dec[i] - 1;     return max;} /* Driver program to test above function */int main(){    int arr[] = {12, 4, 78, 90, 45, 23};    int n = sizeof(arr)/sizeof(arr[0]);    printf("nLength of max length Bitonic Subarray is %d",            bitonic(arr, n));    return 0;}
// Java program to find length of the longest bitonic subarrayimport java.io.*;import java.util.*; class Bitonic{    static int bitonic(int arr[], int n)    {        int[] inc = new int[n]; // Length of increasing subarray ending                                // at all indexes        int[] dec = new int[n]; // Length of decreasing subarray starting                                // at all indexes        int max;         // Length of increasing sequence ending at first index is 1        inc[0] = 1;         // Length of increasing sequence starting at first index is 1        dec[n-1] = 1;         // Step 1) Construct increasing sequence array        for (int i = 1; i < n; i++)           inc[i] = (arr[i] >= arr[i-1])? inc[i-1] + 1: 1;         // Step 2) Construct decreasing sequence array        for (int i = n-2; i >= 0; i--)            dec[i] = (arr[i] >= arr[i+1])? dec[i+1] + 1: 1;         // Step 3) Find the length of maximum length bitonic sequence        max = inc[0] + dec[0] - 1;        for (int i = 1; i < n; i++)            if (inc[i] + dec[i] - 1 > max)                max = inc[i] + dec[i] - 1;         return max;    }     /*Driver function to check for above function*/    public static void main (String[] args)    {        int arr[] = {12, 4, 78, 90, 45, 23};        int n = arr.length;        System.out.println("Length of max length Bitnoic Subarray is "                            + bitonic(arr, n));    }}/* This code is contributed by Devesh Agrawal */
# Python program to find length of the longest bitonic subarray def bitonic(arr, n):         # Length of increasing subarray ending at all indexes    inc = [None] * n         # Length of decreasing subarray starting at all indexes    dec = [None] * n         # length of increasing sequence ending at first index is 1    inc[0] = 1         # length of increasing sequence starting at first index is 1    dec[n-1] = 1     # Step 1) Construct increasing sequence array    for i in range(n):        if arr[i] >= arr[i-1]:            inc[i] = inc[i-1] + 1        else:            inc[i] = 1     # Step 2) Construct decreasing sequence array    for i in range(n-2,-1,-1):        if arr[i] >= arr[i-1]:            dec[i] = inc[i-1] + 1        else:            dec[i] = 1     # Step 3) Find the length of maximum length bitonic sequence    max = inc[0] + dec[0] - 1    for i in range(n):        if inc[i] + dec[i] - 1 > max:            max = inc[i] + dec[i] - 1     return max # Driver program to test above function arr = [12, 4, 78, 90, 45, 23]n = len(arr)print("nLength of max length Bitonic Subarray is ",bitonic(arr, n))
// C# program to find length of the// longest bitonic subarrayusing System; class GFG{    static int bitonic(int []arr, int n)    {        // Length of increasing subarray ending        // at all indexes        int[] inc = new int[n];                 // Length of decreasing subarray starting        // at all indexes        int[] dec = new int[n];                 int max;         // Length of increasing sequence        // ending at first index is 1        inc[0] = 1;         // Length of increasing sequence        // starting at first index is 1        dec[n - 1] = 1;         // Step 1) Construct increasing sequence array        for (int i = 1; i < n; i++)        inc[i] = (arr[i] >= arr[i - 1]) ?                 inc[i - 1] + 1: 1;         // Step 2) Construct decreasing sequence array        for (int i = n - 2; i >= 0; i--)            dec[i] = (arr[i] >= arr[i + 1]) ?                     dec[i + 1] + 1: 1;         // Step 3) Find the length of maximum        // length bitonic sequence        max = inc[0] + dec[0] - 1;        for (int i = 1; i < n; i++)            if (inc[i] + dec[i] - 1 > max)                max = inc[i] + dec[i] - 1;         return max;    }     // Driver function    public static void Main ()    {        int []arr = {12, 4, 78, 90, 45, 23};        int n = arr.Length;        Console.Write("Length of max length Bitonic Subarray is "                      + bitonic(arr, n));    }}// This code is contributed by Sam007
<?php// PHP program to find length of// the longest bitonic subarray function bitonic($arr, $n){    $i; $max;    // length of increasing sequence    // ending at first index is 1    $inc[0] = 1;     // length of increasing sequence    // starting at first index is 1    $dec[$n - 1] = 1;     // Step 1) Construct increasing    // sequence array    for ($i = 1; $i < $n; $i++)    $inc[$i] = ($arr[$i] >= $arr[$i - 1]) ?                          $inc[$i - 1] + 1: 1;     // Step 2) Construct decreasing    // sequence array    for ($i = $n - 2; $i >= 0; $i--)    $dec[$i] = ($arr[$i] >= $arr[$i + 1]) ?                          $dec[$i + 1] + 1: 1;     // Step 3) Find the length of    // maximum length bitonic sequence    $max = $inc[0] + $dec[0] - 1;    for ($i = 1; $i < $n; $i++)        if ($inc[$i] + $dec[$i] - 1 > $max)            $max = $inc[$i] + $dec[$i] - 1;     return $max;} // Driver Code$arr = array(12, 4, 78, 90, 45, 23);$n = sizeof($arr);echo "Length of max length Bitonic " .     "Subarray is ", bitonic($arr, $n);     // This code is contributed by aj_36?>
<script>     // Javascript program to find length of the    // longest bitonic subarray         function bitonic(arr, n)    {        // Length of increasing subarray ending        // at all indexes        let inc = new Array(n);                   // Length of decreasing subarray starting        // at all indexes        let dec = new Array(n);                   let max;           // Length of increasing sequence        // ending at first index is 1        inc[0] = 1;           // Length of increasing sequence        // starting at first index is 1        dec[n - 1] = 1;           // Step 1) Construct increasing sequence array        for (let i = 1; i < n; i++)            inc[i] = (arr[i] >= arr[i - 1]) ? inc[i - 1] + 1: 1;           // Step 2) Construct decreasing sequence array        for (let i = n - 2; i >= 0; i--)            dec[i] = (arr[i] >= arr[i + 1]) ? dec[i + 1] + 1: 1;           // Step 3) Find the length of maximum        // length bitonic sequence        max = inc[0] + dec[0] - 1;        for (let i = 1; i < n; i++)            if (inc[i] + dec[i] - 1 > max)                max = inc[i] + dec[i] - 1;           return max;    }         let arr = [12, 4, 78, 90, 45, 23];    let n = arr.length;    document.write("Length of max length Bitonic Subarray is " + bitonic(arr, n));                       </script>
nLength of max length Bitonic Subarray is 5
Output :  
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
Length of max length Bitnoic Subarray is 5
Time Complexity : O(n) Auxiliary Space : O(n) 
Maximum Length Bitonic Subarray | GeeksforGeeks - YouTubeGeeksforGeeks530K subscribersMaximum Length Bitonic Subarray | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 13:05•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=k4vMVTp6AuI" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Maximum Length Bitonic Subarray | Set 2 (O(n) time and O(1) Space)As an exercise, extend the above implementation to print the longest bitonic subarray also. The above implementation only returns the length of such subarray. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
jit_t
rathbhupendra
ShubhamMaurya3
NishantChahar
rameshtravel07
sweetyty
rajeev0719singh
amartyaghoshgfg
Arrays
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.