Rotate Array right by shift k numbers
Problem statement: You are given an array of size n. Rotate the array by shifting k positions.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll N = 1e5 + 7;
int32_t main(){
ll n, k;
cin >> n >> k;
k = k % n;
vector<ll>v(n);
ll x;
for(ll i = 0; i < n; ++i) {
cin >> x;
v[(i + k) % n] = x;
}
for(auto it: v) cout << it << ' ';
cout << '\n';
//1 2 3 4 5
//5 4 1 2 3
// 1 2 3 4 5
// 5 1 2 3 4
// 4 5 1 2 3
// 3 4 5 1 2
// 2 3 4 5 1
// 1 2 3 4 5
}
Comments
Post a Comment