Right rotate array

 



Cyclically rotate an array by one 
Basic Accuracy: 64.05% Submissions: 33666 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

 

Your Task:  
You don't need to read input or print anything. Your task is to complete the function rotate() which takes the array A[] and its size as inputs and modify the array.

 

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

 

Constraints:
1<=N<=105

0<=a[i]<=105    


#include <iostream>

using namespace std;




int main()

{

    int n;

    cin>>n;

int arr[n];

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

        cin>>arr[i];

    }

    //right shift cyclic rotate

    int temp=arr[n-1];

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

        arr[i]=arr[i-1];

    }

    arr[0]=temp;

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

        cout<<arr[i]<<" ";

    }

}


Comments

Popular posts from this blog

Sum of Even Numbers till N

Find the Runner-Up Score!

Print All Substrings