Cyclically rotate an array by one — GFG

Abhishek Raj
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;
}
}

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response