PROBLEM LINK:
Author: Kunal Demla
Editorialist: Kunal Demla
DIFFICULTY:
CAKEWALK
PREREQUISITES:
Array Indexing
PROBLEM:
Given an Array Arr and an Integer K, print the array after K steps of rotation.
QUICK EXPLANATION:
Traverse the array with j=(i+(size(Arr) -K)) \bmod size(Arr).
EXPLANATION:
It is observable that for any K>N, the apparent K will be K \bmod N.
We can also conclude for any Integer K<N, the element at index N-K comes to 1st index.
Hence we start traversing the array from same.
Using a Loop with, i=0 and i<N, we can conclude the output to be ((N-K)+i), but since the array is still unaltered and non repeating with last index at N, the output shall be made to ((N-K)+i) \bmod N.
ALTERNATE EXPLANATION:
Using another array Brr of size N, we declare Brr[(i+K) \bmod N]= Arr[i] and output Brr.
Actually rotating the whole array is also a method but its non-essential.
SOLUTIONS:
Setter's Solution
#include <iostream>
using namespace std;
#define ll long long int
void solve()
{
int i,j,n,k;
cin>>n>>k;
ll a[n];
for(i=0;i<n;i++)
cin>>a[i];
k=k%n;
for(i=0;i<n;i++)
{
cout<<a[((n-k+i)%(n))]<<" ";
}
cout<<endl;
}
int main() {
int t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}