ALGOQ002 - Editorial

PROBLEM LINK:

Practice

DIFFICULTY:

CakeWalk

PREREQUISITES:

None at all image

PROBLEM:

Given an array, cyclically rotate the array clockwise by one.

EXPLANATION:

Do the following steps:
(1) Store the last element in a variable say x.
(2) Shift all elements one position ahead.
(3) Replace the first element of the array with x.

SOLUTIONS:

Solution
#include <iostream>
using namespace std;

int main()

{

int t;

cin >> t;

while (t--)

{

int n;

cin >> n;

int a[n];
for (int i = 0; i < n; i++)

{

    cin >> a[i];

}

int x = a[n - 1];

for (int i = n - 1; i > 0; i--)

{

    a[i] = a[i - 1];

}

a[0] = x;

for (int i = 0; i < n; i++)

{

    cout << a[i] << " ";

}

cout << endl;

}

return 0;

}