We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Hi All,
I have followed the naive approach.. It takes the time n2... Here is me code... Could you suggest me how I can increase the effeciency and solve it in O(n)..
I am able tos olve 60%...
include
using namespace std;
int main(){
long long int n,m,i,j,t;
cin>>t;
while(t--){
cin>>n;
long long int a[n],g[n],last=0,final=0;
int c=0;
for(i=0;i<n;i++){
cin>>a[i];
}
for(i=0;i<n-1 && c==0 ;i++){
for(j=i+1;j<n;j++){
if(a[i]>a[j]) last++;
if(last>2) break;
}
if(last>2){
//cout<<"too chaotic "<<last<<endl;
c=1;
last=0;
}else{
final=final+last;
last=0;
}
}
if(c==0)
cout<<final<<endl;
else{
cout<<"Too chaotic"<<endl;
}
}
return 0;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
New Year Chaos
You are viewing a single comment's thread. Return to all comments →
Hi All, I have followed the naive approach.. It takes the time n2... Here is me code... Could you suggest me how I can increase the effeciency and solve it in O(n).. I am able tos olve 60%...
include
using namespace std;
int main(){
}