Maximum length of bitonic subarray solution

Solution :-

#include<iostream>
using namespace std;

int main(){
  int t;
  cin>>t;
  int arr[50];
  while(t--){
    int n;
    cin>>n;
    int inc[n],dec[n];
    for(int i=0;i<n;i++){
      cin>>arr[i];
    }
    inc[0]=1;
    dec[n-1]=1;
    for(int i=1;i<n;i++){    ///check elements  are increases from left to right
      inc[i]=arr[i-1]<=arr[i]?inc[i-1]+1:1;
    }
    for(int i=n-2;i>=0;i--){  ///check elements are increases from right to left
      dec[i]=arr[i+1]<=arr[i]?dec[i+1]+1:1;
    }
    int maxLength=inc[0]+dec[0]-1;
    for(int i=0;i<n;i++){
        if(maxLength<=inc[i]+dec[i]-1){
           maxLength=(inc[i]+dec[i])-1;
        }
    }
    cout<<maxLength<<endl;
  }

  return 0;
}

Contact us





JDoodle for WordPress
Scroll Up