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
+2
moryasivam2 days ago
 void reverseInGroups(vector<long long>& arr, int n, int k){     int i=0;     while(i<n){         if(i+k<n){             reverse(arr.begin()+i,arr.begin()+i+k);         }         else{             reverse(arr.begin()+i,arr.end());         }         i+=k;     }   }
 
0
kushwaharajshree233 days ago
C++ solution →
void reverseInGroups(vector<long long>& arr, int n, int k){
        for(int i=0;i<n;i+=k)
        {
            int l=i;
            int r=min(i+k-1, n-1);
            while(l<r)
            {
                int temp= arr[l];
                arr[l]=arr[r];
                arr[r]= temp;
                l++;
                r--;
            }
            
        }
    }
+1
iit20212054 days ago
class Solution{public:   //Function to reverse every sub-array group of size k.   void reverseInGroups(vector<long long>& arr, int n, int k){       int p=n/k;       int l=n%k;       for(int i=0;i<n-l;i=i+k){           reverse(arr.begin()+i,arr.begin()+i+k);       }       reverse(arr.end()-l,arr.end());   }};//easiest approach to an easy problem
0
harshscode5 days ago
most simple amongest all
     for(int i=0;i<n;i=i+k)   {       int l=i;       int r=min(n-1,i+k-1);       while(l<r)       {           int t=a[l];           a[l]=a[r];           a[r]=t;           l++;           r--;       }   }
0
choudharyshraddha125 days ago
class Solution{public:   //Function to reverse every sub-array group of size k.   void reverseInGroups(vector<long long>& arr, int n, int k){       int k1=0;       int perfectgroupsformed=n/k;       for(int i=0;i<perfectgroupsformed;i++)       {           reverse(arr.begin()+(i*k),arr.begin()+(k+k1));           k1+=k;       }       reverse(arr.begin()+(perfectgroupsformed*k),arr.begin()+n);   }};
0
kerim26 days ago
Javascript
 
reverse(arr,n, l, r){
  while(l<r){
     [arr[l],arr[r]] = [arr[r],arr[l]];
     l++, r--;
  }
}
   
reverseInGroups(arr, n, k){
  for(let i = 0; i < n; i+=k){
     if(i+k < n) this.reverse(arr,n,i,i+k-1);
     else this.reverse(arr,n,i,n-1);
  }
}
0
kerim2
This comment was deleted.
+2
deeplakhotia8761 week ago
void reverseInGroups(vector<long long>& arr, int n, int k){       int i=0;       while(i<n){           if(i+k<n){               reverse(arr.begin()+i,arr.begin()+i+k);           }           else{               reverse(arr.begin()+i,arr.end());           }           i+=k;       }   }
0
hharshit81181 week ago
Time Complexity = O(nk);
Space : O(k) 
 void reverseInGroups(vector<long long>& arr, int n, int k){       stack<long long> s;       for(int i=0; i<n; ){           for(int j = 0, l = i; j<k && l<n; j++, l++){               s.push(arr[l]);           }           while(s.empty() == false){               arr[i] = s.top();                s.pop();               i++;           }       }   }
0
tannupriya18991 week ago
//Time Complexity=O(n*n/k)
int start=0;       int end=k-1;       int start1=0;       int end1=k-1;       int cnt=0;       while(start1<n && end1<n)       {           while(start<=end)               swap(arr[start++],arr[end--]);           start1+=k;           end1+=k;           start=start1;           end=end1;           cnt++;       }       start=cnt*k;       end=n-1;       while(start<=end)           swap(arr[start++],arr[end--]);
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.