Reverse array in groups | Practice | GeeksforGeeks

Given an array arr[] of positive integers of size N. Reverse every sub-array group of size K.
 
Example 1:
Input:
N = 5, K = 3
arr[] = {1,2,3,4,5}
Output: 3 2 1 5 4
Explanation: First group consists of elements
1, 2, 3. Second group consists of 4,5.
 
Example 2:
Input:
N = 4, K = 3
arr[] = {5,6,8,9}
Output: 8 6 5 9

 
Your Task:
You don't need to read input or print anything. The task is to complete the function reverseInGroups() which takes the array, N and K as input parameters and modifies the array in-place. 
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
 
Constraints:
1 ≤ N, K ≤ 107
1 ≤ A[i] ≤ 1018
0
atulharsh274Premium7 hours ago
// c++ solution
   int l = 0 , r = min(k , n);           while(l<=n){                    reverse(arr.begin()+l , arr.begin()+r);                    l = l+k;                    r = min(n , r+k);           }
0
nikhil_deore
This comment was deleted.
0
abuzart199912 hours ago
for(int i = 0;i < n; i += k){           if(n - i - 1 < k){               Collections.reverse(arr.subList(i,n));           }                      else{               Collections.reverse(arr.subList(i,i+k));           }       }
0
swetkrmis18 hours ago
class Solution{public:   //Function to reverse every sub-array group of size k.   void reverseInGroups(vector<long long>& arr, int n, int k){       vector<int>v;       int i=0;       int l=i;       int w=k;       while(k<=n){           for(i;i<k;i++){               v.insert(v.begin(),arr[i]);           }           i=l;           int j=0;           for(i;i<k;i++){               arr[i]=v[j];               j++;           }           i=l;           i=i+w;           k=k+w;           l=i;           v.clear();       }       w=i;       if(i<n){           for(i;i<n;i++){               v.insert(v.begin(),arr[i]);           }           int j=0;           i=w;           for(i;i<n;i++){               arr[i]=v[j];               j++;           }       }   }};
0
vinayakunde9620011 day ago
// C++ code
// Time : 0.51/2.07
 
    void reverseInGroups(vector<long long>& arr, int n, int k){       // code here       int s = 0,e = k;       while(e<n){           reverse(arr.begin()+s,arr.begin()+e);           s += k;           e += k;       }       if((n-s)>1) reverse(arr.begin()+s,arr.begin()+n);   }
0
aferd74f71 day ago
PYTHON SOLUTION 
class Solution: #Function to reverse every sub-array group of size k.def reverseInGroups(self, arr, N, K):
  ans=[]  t=0  while(N>0):      if(N<K):         temp=arr[t:]         temp.reverse()         for i in temp:               ans.append(i)         N-=K      else:               temp=arr[t:t+K]               temp.reverse()               for i in temp:                     ans.append(i)               t+=K               N-=K  arr.clear()  for i in ans:        arr.append(i)
0
abhikeshz0kb2 days ago
void reverseInGroups(vector<long long>& v, int n, int k){   int d=0,i;
        for(i=0;i<n&&i+k<n;i=d)
        {
            d=i+k;
            reverse(v.begin()+i,v.begin()+d);
        }
        reverse(v.begin()+i,v.begin()+n);
    }
0
gandhi2520002 days ago
class Solution {   //Function to reverse every sub-array group of size k.   void reverseInGroups(ArrayList<Integer> arr, int n, int k) {       // code here       for(int i=0;i<n;i+=k){                      if(n-i-1<k)               Collections.reverse(arr.subList( i,n));                     else               Collections.reverse(arr.subList( i,i+k ));       }   }}
0
harshbhagwani903 days ago
Expected time complexity=o(n) but most of the solution does not satisfy this .(beacause soln are in o(n*k))
0
abhinayabhi1413 days ago
class Solution {   //Function to reverse every sub-array group of size k.    void swap(ArrayList<Integer> arr , int lo , int hi){      while(lo < hi){          Collections.swap(arr , lo , hi);          lo++;          hi--;      }  }   void reverseInGroups(ArrayList<Integer> arr, int n, int k) {       // code here          int i ;      for(i = 0 ; i+k < n ; i += k){          int l = i;          int r = i + k - 1;          swap(arr , l , r);      }      if(i + k != n){          swap(arr , i , n-1);      }   }}
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.