jasonpogi1669

Rotate elements using C++

Apr 25th, 2022 (edited)
1,028
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.35 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int n;
  7.     cin >> n;
  8.     vector<int> a(n);
  9.     for (int i = 0; i < n; i++) {
  10.         cin >> a[i];
  11.     }
  12.     int p = 4; // no. of adjacent elements to be rotated
  13.     rotate(a.begin(), a.begin() + p, a.begin() + p + 1);
  14.     for (int i = 0; i < n; i++) {
  15.         cout << a[i] << " ";
  16.     }
  17.     cout << '\n';
  18.     return 0;
  19. }
  20.  
Add Comment
Please, Sign In to add comment