Cyclically rotate an array by one | Practice | GeeksforGeeks

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 N as inputs and modify the array in place.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1<=N<=105
0<=a[i]<=105
+1
arbazansary0009in 5 hours
void rotate(int arr[], int n)
{
	// method - 1: [swap method]
     for(int i = n-1; i > 0; i--)
         swap(arr[i], arr[i-1]);
 	
 	// method - 2: [without using swap function]
    // int first = arr[n-1];
    // for(int i = n-1; i >= 0; i--){
    //     arr[i] = arr[i-1];
    // }
    // arr[0] = first;
}
0
addepallibharatsai20011 day ago
void rotate(int arr[], int n){
   for(int i = n-1; i > 0; i--){
       swap(arr[i], arr[i-1]);
   }
}
0
kumarhemendra7851 day ago
void rotate(int arr[], int n){   int temp=arr[0];  for(int i=0;i<n-1;i++)  {      int x=arr[i+1];      arr[i+1]=temp;      temp=x;              }  arr[0]=temp;}
0
singhapmss2 days ago
Python:
def rotate( arr, n):   arr.insert(0,arr[n-1])   arr.pop()   return arr
0
mishravain6u53 days ago
class Compute {      public 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
denishfuletra3 days ago
Simple and Easy solution for java
 
class Compute {      public void rotate(int arr[], int n)   {       int temp = arr[n-1];       for (int i = 0; i<n; i++){           int temp1 = arr[i];           arr[i] = temp;           temp = temp1;       }   }}
+1
mittalikasgar10093 days ago
BEST C++ CODE WITH TIME 0.01.
void rotate(int arr[], int n){   for(int i=0;i<n;i++){       swap(arr[i],arr[n-1]);   }   }
0
peramsaicharanyadav994 days ago
Java Solution
class Compute {      public void rotate(int arr[], int n)   {       LinkedList<Integer> ll=new LinkedList<Integer>();       for(int i:arr){           ll.add(i);       }       for(int i=1;i<=1;i++){           ll.addFirst(arr[n-i]);           ll.removeLast();       }          Iterator<Integer> it=ll.iterator();           int i=0;       while(it.hasNext()){           arr[i]=it.next();           i++;       }   }}
0
candychandra3464 days ago
Cpp/C code for Big-endian to little-endian conversion.
0
amankhandelwal4 days ago
class Compute {      public void rotate(int arr[], int n)   {       int temp;       for(int i=n-1;i>0;i--)       {           temp = arr[i];           arr[i] = arr[i-1];           arr[i-1] = temp;       }   }}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.