New Year Chaos

  • + 0 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(){

    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;
    

    }