Reverse an array in groups of given size | Set 3 (Single traversal)

13 Aug, 2021
Given an array, reverse every sub-array formed by consecutive k elements.
Examples: 
Input: arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3. Output: [3, 2, 1, 6, 5, 4, 9, 8, 7, 10]Input: arr = [1, 2, 3, 4, 5, 6, 7], k = 5. Output: [5, 4, 3, 2, 1, 7, 6]
Approach: 
We will use two pointers technique to solve this problem.First, we will initialize our 1st pointer d with value k-1 (d = k-1) and a variable m with value 2 (m = 2).Now, we will iterate the array with our 2nd pointer i and check If i < d, Swap (arr[i], arr[d]) and decrement d by 1. Otherwise,Make d = k * m – 1, i = k * (m – 1) – 1 and m = m + 1.
We will use two pointers technique to solve this problem.
First, we will initialize our 1st pointer d with value k-1 (d = k-1) and a variable m with value 2 (m = 2).
Now, we will iterate the array with our 2nd pointer i and check If i < d, Swap (arr[i], arr[d]) and decrement d by 1. Otherwise,Make d = k * m – 1, i = k * (m – 1) – 1 and m = m + 1.
If i < d, Swap (arr[i], arr[d]) and decrement d by 1. Otherwise,
Make d = k * m – 1, i = k * (m – 1) – 1 and m = m + 1.
Below is the implementation of the above approach. 
C++
C
Java
Python3
C#
Javascript
// C++ program to reverse every sub-array// formed by consecutive k elements#include<bits/stdc++.h>using namespace std; // Function to reverse every sub-array// formed by consecutive k elementsvoid ReverseInGroup(int arr[], int n, int k){    if(n < k)    {        k = n;    }     // Initialize variables    int d = k - 1, m = 2;    int i = 0;             for(i = 0; i < n; i++)    {               if (i >= d)       {           // Update the variables           d = k * (m);           if(d >= n)           {               d = n;                   }               i = k * (m - 1) - 1;           m++;       }       else       {           int t = arr[i];           arr[i] = arr[d];           arr[d] = t;       }       d = d - 1;    }    return;} // Driver codeint main(){    int arr[] = { 1, 2, 3, 4, 5, 6, 7 };    int k = 3;    int n = sizeof(arr) / sizeof(arr[0]);     ReverseInGroup(arr, n, k);     for(int i = 0; i < n; i++)       cout << arr[i] << " ";     return 0;} // This code is contributed by Code_Mech
// C program to reverse every sub-array// formed by consecutive k elements#include<stdio.h> // Function to reverse every sub-array// formed by consecutive k elementsvoid ReverseInGroup(int arr[], int n, int k){    if(n<k)    {       k=n;    }     // Initialize variables    int d = k-1, m=2;    int i = 0;             for (i = 0; i < n; i++)    {        if (i >= d)        {              // Update the variables            d = k * (m);            if(d>=n)            {                d = n;                        }                      i = k * (m - 1)-1;            m++;                     }        else        {            int t = arr[i];            arr[i] = arr[d];            arr[d] = t;        }      d = d - 1;                 }    return;         } // Driver codeint main(){    int arr[] = {1, 2, 3, 4, 5, 6, 7};    int k = 3;       int n = sizeof(arr) / sizeof(arr[0]);       ReverseInGroup(arr, n, k);       for (int i = 0; i < n; i++)        printf("%d ", arr[i]);       return 0;}
// Java program to reverse every sub-array// formed by consecutive k elementsclass GFG{     // Function to reverse every sub-array// formed by consecutive k elementsstatic void ReverseInGroup(int arr[],                           int n, int k){    if(n < k)    {        k = n;    }     // Initialize variables    int d = k - 1, m = 2;    int i = 0;             for(i = 0; i < n; i++)    {       if (i >= d)       {                       // Update the variables           d = k * (m);           if(d >= n)           {               d = n;                       }                   i = k * (m - 1) - 1;           m++;       }       else       {           int t = arr[i];           arr[i] = arr[d];           arr[d] = t;       }       d = d - 1;        }    return;} // Driver codepublic static void main(String[] args){    int arr[] = { 1, 2, 3, 4, 5, 6, 7 };    int k = 3;    int n = arr.length;         ReverseInGroup(arr, n, k);         for(int i = 0; i < n; i++)       System.out.printf("%d ", arr[i]);}} // This code is contributed by sapnasingh4991
# Python3 program to reverse# every sub-array formed by# consecutive k elements # Function to reverse every# sub-array formed by consecutive# k elementsdef ReverseInGroup(arr, n, k):     if(n < k):        k = n     # Initialize variables    d = k - 1    m = 2    i = 0     while i < n:        if (i >= d):             # Update the            # variables            d = k * (m)                         if(d >= n):                d = n             i = k * (m - 1) - 1            m += 1         else:            arr[i], arr[d] = (arr[d],                              arr[i])        d = d - 1        i += 1     return # Driver codeif __name__ == "__main__":     arr = [1, 2, 3,           4, 5, 6, 7]    k = 3    n = len(arr)    ReverseInGroup(arr, n, k)     for i in range(n):        print(arr[i],              end = " ") # This code is contributed by Chitranayal
// C# program to reverse every sub-array// formed by consecutive k elementsusing System;class GFG{     // Function to reverse every sub-array// formed by consecutive k elementsstatic void ReverseInGroup(int []arr,                           int n, int k){    if(n < k)    {        k = n;    }     // Initialize variables    int d = k - 1, m = 2;    int i = 0;             for(i = 0; i < n; i++)    {        if (i >= d)        {                             // Update the variables            d = k * (m);            if(d >= n)            {                d = n;                        }                    i = k * (m - 1) - 1;            m++;        }        else        {            int t = arr[i];            arr[i] = arr[d];            arr[d] = t;        }        d = d - 1;        }    return;} // Driver codepublic static void Main(){    int []arr = { 1, 2, 3, 4, 5, 6, 7 };    int k = 3;    int n = arr.Length;         ReverseInGroup(arr, n, k);         for(int i = 0; i < n; i++)        Console.Write(arr[i] + " ");}} // This code is contributed by Code_Mech
<script>// Javascript program to reverse every sub-array// formed by consecutive k elements  // Function to reverse every sub-array// formed by consecutive k elementsfunction ReverseInGroup(arr, n, k){    if(n < k)    {        k = n;    }     // Initialize variables    let d = k - 1, m = 2;    let i = 0;             for(i = 0; i < n; i++)    {                 if (i >= d)        {            // Update the variables            d = k * (m);            if(d >= n)            {              d = n;                   }               i = k * (m - 1) - 1;            m++;        }        else        {            let t = arr[i];            arr[i] = arr[d];            arr[d] = t;        }        d = d - 1;    }    return;} // Driver code     let arr = [ 1, 2, 3, 4, 5, 6, 7 ];    let k = 3;    let n = arr.length;     ReverseInGroup(arr, n, k);     for(let i = 0; i < n; i++)    document.write(arr[i] + " ");  // This code is contributed by _saurabh_jaiswal</script>
3 2 1 6 5 4 7
 
Time Complexity: O(N) Auxiliary Space: O(1) 
sapnasingh4991
Code_Mech
ukasp
_saurabh_jaiswal
pankajsharmagfg
Arrays
Data Structures
Data Structures
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
Search, insert and delete in an unsorted array
Window Sliding Technique
Chocolate Distribution Problem
Find duplicates in O(n) time and O(1) extra space | Set 1
DSA Sheet by Love Babbar
SDE SHEET - A Complete Guide for SDE Preparation
What is Hashing | A Complete Tutorial
Introduction to Data Structures
Doubly Linked List | Set 1 (Introduction and Insertion)