Ankit_132

Swap the numbers

Jun 28th, 2023
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. #define ll     long long
  6. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  7. #define pb     push_back
  8.  
  9. int main()
  10. {
  11.     _test
  12.     {
  13.         int n, k;
  14.         cin>>n>>k;
  15.  
  16.         vector<int> a(n);
  17.         for(auto &e: a)
  18.             cin>>e;
  19.  
  20.         if(k >= n)
  21.         {
  22.             for(auto e: a)
  23.                 cout<<e<<" ";
  24.             cout<<"\n";
  25.         }
  26.         else
  27.         {
  28.  
  29.             vector<int> v;
  30.             for(int i=0; i<n; i++)
  31.             {
  32.                 if(i<k && (n-1-i)<k)        continue;
  33.                 v.pb(a[i]);
  34.             }
  35.  
  36.             sort(v.begin(), v.end());
  37.  
  38.             for(int i=0, j=0; i<n; i++)
  39.             {
  40.                 if(i<k && (n-1-i)<k)        continue;
  41.                 a[i] = v[j++];
  42.             }
  43.  
  44.             for(auto e: a)
  45.                 cout<<e<<" ";
  46.             cout<<"\n";
  47.         }
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment