Cyclically rotate an array by one - 450DSA Cracker Solution

Akshima SharmaAkshima Sharma
1 min read

Cyclically rotate an array by one

Given an array, rotate the array by one position in clock-wise direction.

Example 1:

Input:
N = 5
A[] = {1, 2, 3, 4, 5}
Output:
5 1 2 3 4

Example 2:

Input:
N = 8
A[] = {9, 8, 7, 6, 4, 2, 1, 3}
Output:
3 9 8 7 6 4 2 1

Code

void rotate(int arr[], int n)
{
    int temp = arr[n-1];
    for(int i=n-1;i>0;i--)
        arr[i] = arr[i-1];
    arr[0]= temp;
}
0
Subscribe to my newsletter

Read articles from Akshima Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Akshima Sharma
Akshima Sharma