Subscribe Us

Responsive Advertisement

Advertisement

1638A Reverse codeforces solution in cpp

 1638A  Reverse codeforces solution in cpp

by ujjal roy





#include<bits/stdc++.h>

using namespace std;

main()

{

    int t,n,i,j;

    cin>>t;

    while(t--)

    {

        int c=1,ch=0;

        cin>>n;

    vector<int> a(n);

        for(i=0;i<n;i++) cin>>a[i];


      for(i=0;i<n;i++)

      {

          if(c==a[i])

          {

              c++;

              continue;

          }

          else

          {

              for(j=i;j<n;j++)

              {

                  if(a[j]==c)

                  {

                       reverse(a.begin()+i,a.begin()+j+1);

                       ch=1;

                       break;


                  }

              }

          }

          if(ch==1) break;

      }



          for(i=0;i<n;i++)

          {

              cout<<a[i]<<" ";

          }

          cout<<endl;

    }




    return 0;

}


Post a Comment

0 Comments