Cyclically rotate an array by one — GFG
Jul 31, 2021
Given an array, rotate the array by one position in clockwise 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
Java Solution :-
class Compute {
public void rotate(int arr[], int n)
{
int temp=arr[n-1];
for(int i=n-2;i>=0;i — )
{
arr[(i+1)%n]=arr[i];
}
arr[0]=temp;
}
}