Reverse an Array in groups of given size
Given an array arr[] and an integer k, the task is to reverse every subarray 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, 7Input: arr[] = [1, 2, 3, 4, 5, 6, 7, 8], k = 5
Output: 5, 4, 3, 2, 1, 8, 7, 6Input: arr[] = [1, 2, 3, 4, 5, 6], k = 1
Output: 1, 2, 3, 4, 5, 6Input: arr[] = [1, 2, 3, 4, 5, 6, 7, 8], k = 10
Output: 8, 7, 6, 5, 4, 3, 2, 1
Approach:
The problem can be solved based on the following idea:
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 a 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.
To reverse a subarray, maintain two pointers: left and right. Now, swap the elements at left and right pointers and increment left by 1 and decrement right by 1. Repeat till left and right pointers don’t cross each other.
Working:








#include <iostream>
#include <vector>
using namespace std;
// Function to reverse every sub-array formed by
// consecutive k elements
void reverseInGroups(vector<int>& arr, int k) {
int n = arr.size(); // Get the size of the array
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 code
int main()
{
vector<int> arr = {1, 2, 3, 4, 5, 6, 7, 8}; // Input array
int k = 3; // Size of sub-arrays to be reversed
reverseInGroups(arr, k); // Call function to reverse in groups
// Print modified array
for (int num : arr)
cout << num << " ";
return 0;
}
// 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 elements
void 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 code
int 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
import java.util.*;
class GFG {
// Function to reverse every sub-array of size k
static void reverseInGroups(int[] arr, int k) {
int n = arr.length;
for (int i = 0; i < n; i += k) {
int left = i;
int right = Math.min(i + k - 1, n - 1);
// Reverse the sub-array
while (left < right) {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
int k = 3;
reverseInGroups(arr, k);
// Print modified array
for (int num : arr) {
System.out.print(num + " ");
}
}
}
# Python 3 program to reverse every
# sub-array formed by consecutive k
# elements
# Function to reverse every sub-array
# formed by consecutive k elements
def reverseInGroups(arr, k):
i = 0
n = len(arr) # Get the size of the array
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
arr = [1, 2, 3, 4, 5, 6, 7, 8]
k = 3
reverseInGroups(arr, k)
# Print modified array
print(" ".join(map(str, arr)))
// C# program to reverse every sub-array
// formed by consecutive k elements
using System;
class GFG
{
// Function to reverse every sub-array
// formed by consecutive k elements
public 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 Code
public 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] + " ");
}
}
}
// Javascript program to reverse every sub-array
// formed by consecutive k elements
// Function to reverse every sub-array
// formed by consecutive k elements
function reverseInGroups(arr, k) {
let n = arr.length;
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);
// Reverse the sub-array [left, right]
while (left < right) {
// Swap elements
[arr[left], arr[right]] = [arr[right], arr[left]];
left += 1;
right -= 1;
}
}
return arr;
}
// Driver Code
let arr = [1, 2, 3, 4, 5, 6, 7, 8];
let k = 3;
let arr1 = reverseInGroups(arr, k);
// Print modified array
console.log(arr1.join(" "));
Output
3 2 1 6 5 4 8 7
Time complexity: O(n)
Auxiliary space: O(1)