Problem no 7: Cyclically rotate the array by one.

 

Cyclically rotate an array by one
Basic Accuracy: 77.14% Submissions: 4605 Points: 1

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
 
 
Answer:
 
void rotate(int arr[], int n)
{
int num=arr[n-1];
for(int i=n-1;i>0;i--)
{
arr[i]=arr[i-1];
}
arr[0]=num;


for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}

Comments

Popular posts from this blog

Problem no 18(array):count the number of pairs in array whose sum is equal to the given number.

convert the integer number to its binary representation.

Tcs assessment problem: museum problem.