Rotating an Array | Practice | GeeksforGeeks

Given an array of size N. The task is to rotate array by D elements where D ≤ N.
Example 1:
Input:
N = 7
Arr[] = {1, 2, 3, 4, 5, 6, 7}
D = 2
Output: 3 4 5 6 7 1 2
Explanation: 
Rotate by 1: [2, 3, 4, 5, 6, 7, 1]
Rotate by 2: [3, 4, 5, 6, 7, 1, 2]


Example 2:
Input:
N = 4
Arr[] = {1, 3, 4, 2}
D = 3
Output: 2 1 3 4


Your Task:
You don't need to read input or print anything. Your task is to complete the function leftRotate() which takes the array of integers arr[], its size n and d as input parameters and rotates arr[] in-place without using any extra memory.

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

Constraints:
1 ≤ N ≤ 105
1 ≤ Arr[i] ≤ 1000
0 ≤ D ≤ N
 
0
arvind16yadav17 hours ago
/*JAVA CODE*/
void leftRotate(int[] arr, int n, int d) {       // code here              int brr[] = new int[d];              for(int i=0; i<d; i++){           brr[i] = arr[i];       }              for(int i=d; i<n; i++){           arr[i-d] = arr[i];       }              for(int i=0; i<d; i++){           arr[n-d+i] = brr[i];       }          }
0
darshananimus3 days ago
More optimized approach
n/2 = time complexity
class Solution {
    static int[] reverseArray(int[] arr,int start,int end)
    {
        int temp=0;
        int mid = (start+end)/2;
        for(int i=start;i<=mid;i++)
        {
            temp=arr[i];
            arr[i]=arr[end];
            arr[end]=temp;
            end--;
        }
        // while(start<end)
        // {
        //     temp=arr[start];
        //     arr[start]=arr[end];
        //     arr[end]=temp;
        //     start++;
        //     end--;
        // }
        return arr;
    }
    void leftRotate(int[] arr, int n, int d) {
        if(d>0)//corner case handling
            reverseArray(arr,0,d-1);
        reverseArray(arr,d,n-1);
        reverseArray(arr,0,n-1);
    }
}
0
polamreddyn6 days ago
class Solution {  void leftRotate(int arr[], int n, int d) {   // code here   d%=n;      rotation(arr, 0, d-1);      rotation(arr, d, n-1);      rotation(arr, 0, n-1);}
void rotation(int array[], int start, int end) {      int temp;      while(start < end) {       temp = array[start];       array[start] = array[end];       array[end] = temp;              start++;       end --;   }      }
0
div20
This comment was deleted.
+1
surajpatil39412 weeks ago
Simple One line approach in C++
 
rotate(arr, arr+d, arr+n);
0
akshayjamodkar3 weeks ago
Using reversing 
0
sahillather0023 weeks ago
Simple way for rotation of array using RECURSIVE Calls:
void reverse(int arr[],int low,int high){   int temp;       while(low<high){           temp = arr[low];           arr[low]=arr[high];           arr[high]=temp;                      low++;           high--;       }   }
void leftRotate(int arr[], int n, int d) {   // code here   reverse(arr,d,n-1);   reverse(arr,0,d-1);   reverse(arr,0,n-1);}
0
dheerukd20023 weeks ago
C++ Solution : Time=O(n) Space=O(1)
void leftRotate(int arr[], int n, int d) {       // For preserving the original element       int M=*max_element(arr,arr+n)+1;       //Now we contain both original(obtained by e%M) and required elements(obtained by e/M) in each array element.       for(int i=0;i<n;i++){           arr[i]+=(arr[(i+d)%n]%M)*M;       }              for(int i=0;i<n;i++){           arr[i]/=M;       }   }
0
hasnainraza1998hr1 month ago
C++, 0.1
void leftRotate(int arr[], int n, int d) {   int brr[d];   int index = 0;   for(int i=0;i<d;i++){       brr[i]=arr[i];   }   for(;index<n-d;index++){       arr[index]=arr[index+d];   }   for(int i=0;i<d;i++){       arr[index++]=brr[i];   }}
+3
amiransarimy1 month ago
One Line Solutions
 
def leftRotate(self, arr, n, d):       arr[:] = arr[d:]+arr[:d]
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.