Reverse an array in groups of given size

01 Jun, 2022
Given an array, reverse every sub-array formed by consecutive k elements.
Examples: 
Input: arr = [1, 2, 3, 4, 5, 6, 7, 8, 9] k = 3 Output: [3, 2, 1, 6, 5, 4, 9, 8, 7]
Input: arr = [1, 2, 3, 4, 5, 6, 7, 8] k = 5 Output: [5, 4, 3, 2, 1, 8, 7, 6]
Input: arr = [1, 2, 3, 4, 5, 6] k = 1 Output: [1, 2, 3, 4, 5, 6]
Input: arr = [1, 2, 3, 4, 5, 6, 7, 8] k = 10 Output: [8, 7, 6, 5, 4, 3, 2, 1] 
Approach: Consider every sub-array of size k starting from the beginning of the array and reverse it. We need to handle some special cases. If k is not multiple of n where n is the size of the array, for the last group we will have less than k elements left, we need to reverse all remaining elements. If k = 1, the array should remain unchanged. If k >= n, we reverse all elements present in the array.
Below image is a dry run of the above approach: 
Below is the implementation of the above approach:
C++
Java
Python3
C#
PHP
Javascript
C
// C++ program to reverse every sub-array formed by// consecutive k elements#include <iostream>using namespace std; // Function to reverse every sub-array formed by// consecutive k elementsvoid reverse(int arr[], int n, int k){    for (int i = 0; i < n; i += k)    {        int left = i;         // to handle case when k is not multiple of n        int right = min(i + k - 1, n - 1);         // reverse the sub-array [left, right]        while (left < right)            swap(arr[left++], arr[right--]);     }} // Driver codeint main(){    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};    int k = 3;     int n = sizeof(arr) / sizeof(arr[0]);     reverse(arr, n, k);     for (int i = 0; i < n; i++)        cout << 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 elements    static void reverse(int arr[], int n, int k)    {        for (int i = 0; i < n; i += k)        {            int left = i;                 // to handle case when k is not multiple            // of n            int right = Math.min(i + k - 1, n - 1);            int temp;                         // reverse the sub-array [left, right]            while (left < right)            {                temp=arr[left];                arr[left]=arr[right];                arr[right]=temp;                left+=1;                right-=1;            }        }    }         // Driver method    public static void main(String[] args)    {                 int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};        int k = 3;             int n = arr.length;             reverse(arr, n, k);             for (int i = 0; i < n; i++)            System.out.print(arr[i] + " ");    }} // This code is contributed by Anant Agarwal.
# Python 3 program to reverse every# sub-array formed by consecutive k# elements # Function to reverse every sub-array# formed by consecutive k elementsdef reverse(arr, n, k):    i = 0         while(i<n):             left = i         # To handle case when k is not        # multiple of n        right = min(i + k - 1, n - 1)         # Reverse the sub-array [left, right]        while (left < right):                         arr[left], arr[right] = arr[right], arr[left]            left+= 1;            right-=1        i+= k     # Driver codearr = [1, 2, 3, 4, 5, 6,                   7, 8] k = 3n = len(arr)reverse(arr, n, k) for i in range(0, n):        print(arr[i], end =" ")         # This code is contributed by Smitha Dinesh Semwal
// 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 elementspublic static void reverse(int[] arr,                           int n, int k){    for (int i = 0; i < n; i += k)    {        int left = i;         // to handle case when k is        // not multiple of n        int right = Math.Min(i + k - 1, n - 1);        int temp;         // reverse the sub-array [left, right]        while (left < right)        {            temp = arr[left];            arr[left] = arr[right];            arr[right] = temp;            left += 1;            right -= 1;        }    }} // Driver Codepublic static void Main(string[] args){    int[] arr = new int[] {1, 2, 3, 4,                           5, 6, 7, 8};    int k = 3;     int n = arr.Length;     reverse(arr, n, k);     for (int i = 0; i < n; i++)    {        Console.Write(arr[i] + " ");    }}} // This code is contributed// by Shrikant13
<?php// PHP program to reverse every sub-array// formed by consecutive k elements     // Function to reverse every sub-array// formed by consecutive k elementsfunction reverse($arr, $n, $k){    for ($i = 0; $i < $n; $i += $k)    {        $left = $i;         // to handle case when k is not        // multiple of n        $right = min($i + $k - 1, $n - 1);        $temp;                 // reverse the sub-array [left, right]        while ($left < $right)        {            $temp = $arr[$left];            $arr[$left] = $arr[$right];            $arr[$right] = $temp;            $left += 1;            $right -= 1;        }    }    return $arr;} // Driver Code$arr = array(1, 2, 3, 4, 5, 6, 7, 8);$k = 3; $n = sizeof($arr); $arr1 = reverse($arr, $n, $k); for ($i = 0; $i < $n; $i++)    echo $arr1[$i] . " "; // This code is contributed// by Akanksha Rai?>
<script> // Javascript program to reverse every sub-array// formed by consecutive k elements     // Function to reverse every sub-array// formed by consecutive k elementsfunction reverse(arr, n, k){    for(let i = 0; i < n; i += k)    {        let left = i;         // To handle case when k is not        // multiple of n        let right = Math.min(i + k - 1, n - 1);        let temp;                 // Reverse the sub-array [left, right]        while (left < right)        {            temp = arr[left];            arr[left] = arr[right];            arr[right] = temp;            left += 1;            right -= 1;        }    }    return arr;} // Driver Codelet arr = new Array(1, 2, 3, 4, 5, 6, 7, 8);let k = 3;let n = arr.length;let arr1 = reverse(arr, n, k); for(let i = 0; i < n; i++)    document.write(arr1[i] + " "); // This code is contributed by saurabh jaiswal </script>
// 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 reverse(int arr[], int n, int k){    for (int i = 0; i < n; i += k)    {        int left = i;        int right;        // to handle case when k is not multiple of n        if(i+k-1<n-1)        right = i+k-1;        else        right = n-1;         // reverse the sub-array [left, right]        while (left < right)            {                // swap                int temp = arr[left];                arr[left] = arr[right];                arr[right] = temp;                left++;                right--;            }     }} // Driver codeint main(){    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};    int k = 3;     int n = sizeof(arr) / sizeof(arr[0]);     reverse(arr, n, k);     for (int i = 0; i < n; i++)        printf("%d ",arr[i]);     return 0;}//  This code is contributed by Arpit Jain
Output: 
3 2 1 6 5 4 8 7
Time complexity of above solution is O(n). Auxiliary space used by the program is O(1).
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
shrikanth13
Akanksha_Rai
ilamparithi karthike
_saurabh_jaiswal
111arpit1
Amazon
Marketing
Reverse
Arrays
Amazon
Arrays
Reverse
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.