/* BITONIC
1) first increasing then decreasing , or
2) entirely increasing , or
3) entirely decreasing */
#include<iostream>
#include<climits>
using namespace std;
bool checkbitonic(int a[],int i,int j)
{ int start=i;
int end=j;
bool decreasing=false;
int flag=0;
for ( start = i; start <=end; start++)
{
if (a[start]>a[start+1]&&decreasing==false&&start+1<=end)
{
decreasing=true;
int flag=1;
}
if (a[start]<a[start+1]&&decreasing&&start+1<end)
{
return false;
}
}
return true;
}
void maxlengthbitonic()
{ int max_len=INT_MIN;
int n;
cin>>n;
int a[1000]={0};
for (int i = 0; i < n ; i++)
{
cin>>a[i];
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if(checkbitonic(a,i,j))
{
int leng=(j-i)+1;
max_len=max(leng,max_len);
}
}
}
cout<<max_len<<endl;
}
int main(){
int t;
cin>>t;
while (t)
{
maxlengthbitonic();
t--;
}
return 0;
}
Cant pass all test caes
hello @sagar_aggarwal
a) declare ur array as
int arr[n];
b) use <= and >= ( refer question equality sign is there)
c) start j from i. not from 0 .
d)
your solution time complexity is O(n^3) .
in worst case it will give time limit exceeded.
try to think of some O(n) approach.
1 Like
I am still thinking for o(n) approch but can you please tell me why this code is still showing run error even after changes
/* BITONIC
1) first increasing then decreasing , or
2) entirely increasing , or
3) entirely decreasing */
#include<iostream>
#include<climits>
using namespace std;
bool checkbitonic(int a[],int i,int j)
{ int start=i;
int end=j;
bool decreasing=false;
int flag=0;
for ( start = i; start <=end; start++)
{ if (a[start]==a[start+1]&&start+1<=end)
{
return false;
}
if (a[start]>a[start+1]&&decreasing==false&&start+1<=end)
{
decreasing=true;
int flag=1;
}
if (a[start]<a[start+1]&&decreasing==true&&start+1<=end)
{
return false;
}
}
return true;
}
void maxlengthbitonic()
{ int max_len=INT_MIN;
int n;
cin>>n;
int a[n]={0};
for (int i = 0; i < n ; i++)
{
cin>>a[i];
}
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
if(checkbitonic(a,i,j))
{
int leng=(j-i)+1;
max_len=max(leng,max_len);
}
}
}
cout<<max_len<<endl;
}
int main(){
int t;
cin>>t;
while (t)
{
maxlengthbitonic();
t--;
}
return 0;
}

check this condition first and then any other condition