Rotate Array | Practice | GeeksforGeeks

Given an unsorted array arr[] of size N. Rotate the array to the left (counter-clockwise direction) by D steps, where D is a positive integer. 
 
Example 1:
Input:
N = 5, D = 2
arr[] = {1,2,3,4,5}
Output: 3 4 5 1 2
Explanation: 1 2 3 4 5  when rotated
by 2 elements, it becomes 3 4 5 1 2.
Example 2:
Input:
N = 10, D = 3
arr[] = {2,4,6,8,10,12,14,16,18,20}
Output: 8 10 12 14 16 18 20 2 4 6
Explanation: 2 4 6 8 10 12 14 16 18 20 
when rotated by 3 elements, it becomes 
8 10 12 14 16 18 20 2 4 6.

 
Your Task:
Complete the function rotateArr() which takes the array, D and N as input parameters and rotates the array by D elements. The array must be modified in-place without using extra space. 
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 106
1 <= D <= 106
0 <= arr[i] <= 105
+1
kartikeyashokgautam6 hours ago
JAVA Solution :-
 
    static void rotateArr(int arr[], int d, int n)   {    d = d%n;    reverse(arr,0,d-1);    reverse(arr,d,n-1);    reverse(arr,0,n-1);   }    public static void reverse(int[] arr, int start, int end) {   while (start < end) {       int temp = arr[start];       arr[start] = arr[end];        arr[end] = temp;       start++;       end--;   }}
 
 
0
chauhanankur78023 hours ago
 int t[n],i;   d=d%n;   for(i=0;i<n;i++)   {       t[i]=arr[i];   }   for(i=0;i<n;i++)   {       arr[(n-d+i)%n]=t[i];   }
0
shanu144 days ago
C++ Solution :-
void rotateArr(int arr[], int d, int n){
        d=d%n;
        int a[d];
        for(int i=0;i<d;i++){
            a[i]=arr[i];
        }
        for(int i=0;i<n-d;i++){
            arr[i]=arr[i+d];
        }
        int s=0;
        for(int i=n-d;i<n;i++){
            arr[i]=a[s++];
        }
    }
0
animeshmishra97
This comment was deleted.
0
polamreddyn5 days ago
class Solution{   //Function to rotate an array by d elements in counter-clockwise direction.    static void rotateArr(int arr[], int d, int n)   {       // add your code here      d%=n;       //First reversing d elements from starting index.       rvereseArray(arr, 0, d-1);       //Then reversing the last n-d elements.       rvereseArray(arr, d, n - 1);        //Finally, reversing the whole array.       rvereseArray(arr, 0, n - 1);           }      static void  rvereseArray(int arr[], int start, int end){       int temp;
       while (start < end) {                        temp = arr[start];            arr[start] = arr[end];            arr[end] = temp;                      start++;            end--;        }    }}
-1
arijitkarmakar0565 days ago
can anyone tell me whare can i improve my code
 
 
class Solution{   //Function to rotate an array by d elements in counter-clockwise direction.       static void rotateArr(int arr[], int d, int n)   {        // add your code here[]       if(n < d) {           d = d - n;       }       int index = 0;       int temp[] = new int[d];       for(int i = 0; i < d; i++) {           temp[i] = arr[i];       }       for(int i = 0; i < arr.length - d ; i++, index = i) {           arr[i] = arr[i + d];       }       for(int i = 0; i < d; i++) {           arr[index++] =  temp[i];       }   }}
+1
sandeep55216 days ago
C++ Easy to understand O(n) Solution
void rev(int nums[],int s,int l){
       while(s<l) swap(nums[s++],nums[l--]);
   }
   void rotateArr(int nums[], int k, int n){
       // code here
       if(!k) return;
       k=k%n;
       rev(nums,0,k-1);
       rev(nums,k,n-1);
       rev(nums,0,n-1);
   }
0
arijitkarmakar056
This comment was deleted.
0
mannukhurana103971 week ago
I have tried 2 solution  but only 130 test cases pass out of 150,  
 
 
    static void rotateArr(int arr[], int d, int n)
    {
        d = d%n;
        for(int i=0; i<d; i++)
            rotate(arr, n);  
            
        // Time: O(3n)
        
    }
    
    static void rotate(int[] arr, int n){
        int temp = arr[0];
        for(int i=1; i<n; i++)
            arr[i-1] = arr[i];
        arr[n-1] = temp;
    }
    
//   -----------------------------------------------------------------
    
    static void rotateArr(int arr[], int d, int n){
        d = d%n;
        
        rotate(arr, 0, d-1);  // O(d)
        rotate(arr, d, n-1); // O(n-d) 
        rotate(arr, 0, n-1); // O(n) 
        
        // Time : O(2n)
        
    }
    
    public static void rotate(int[] arr, int a, int b){
        while(a<b){
            if(a-b!=0){ //reach center
                int temp = arr[a];
                arr[a] = arr[b];
                arr[b] = temp;
            }
            a++;
            b--;
        }
    }
0
arshnoor1656be21
This comment was deleted.
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.