cyclic array rotation in cpp

Solutions on MaxInterview for cyclic array rotation in cpp by the best coders in the world

showing results for - "cyclic array rotation in cpp"
Luce
03 Oct 2018
1// Array Rotations
2
3#include <bits/stdc++.h>
4
5using namespace std;
6
7void arrayRotation(int arr[], int n){
8    int last = arr[n-1];
9    for(int i=n-1;i>0;i--)
10    arr[i] = arr[i-1];
11    arr[0] = last;
12}
13
14int main(){
15    int n,i,turns;
16    // size of the array
17    cin >> n;
18    
19    int arr[n];
20    for(i=0;i<n;i++)
21    cin >> arr[i];
22
23    // Number of times the array is to be rotated
24    cin >> turns;
25
26    for(i=0;i<turns;i++)
27    arrayRotation(arr,n);
28
29    for(i=0;i<n;i++)
30    cout << arr[i] << " ";
31
32    return 0;
33
34}