Given an array arr[] and an integer k, find the array after reversing every subarray of consecutive k elements in place. If the last subarray has fewer than k elements, reverse it as it is. Modify the array in place, do not return anything.
Examples:
Input: arr[] = [1, 2, 3, 4, 5, 6, 7, 8], k = 3
Output: [3, 2, 1, 6, 5, 4, 8, 7]
Explanation: Elements is reversed: [1, 2, 3] → [3, 2, 1], [4, 5, 6] → [6, 5, 4], and the last group [7, 8](size < 3) is reversed as [8, 7].Input: arr[] = [1, 2, 3, 4, 5], k = 3
Output: [3, 2, 1, 5, 4]
Explanation: First group consists of elements 1, 2, 3. Second group consists of 4, 5.Input: arr[] = [5, 6, 8, 9], k = 5
Output: [9, 8, 6, 5]
Explanation: Since k is greater than array size, the entire array is reversed.
Fixed-Size Group Reversal–Time O(n) and Space O(1)
Edge Cases:
- When
k = 1, the array stays the same - When
kis greater than or equal to the array size
Approach
- We begin from index 0 and find the size of the current subarray to be reversed. If the number of remaining elements at the end is less than
k, reverse all of them. - Each subarray is reversed using two pointers that start from the two corners of the subarray.
#include <iostream>
#include <vector>
using namespace std;
void reverseInGroups(vector<int>& arr, int k){
int n = arr.size();
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--]);
}
}
}
int main() {
vector<int> arr = {1, 2, 3, 4, 5, 6, 7, 8};
int k = 3;
reverseInGroups(arr, k);
for (int num : arr)
cout << num << " ";
return 0;
}
#include <stdio.h>
void reverseInGroups(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--;
}
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
int k = 3;
int n = sizeof(arr) / sizeof(arr[0]);
reverseInGroups(arr, n, k);
for (int i = 0; i < n; i++)
printf("%d ",arr[i]);
return 0;
}
class GfG {
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);
for (int num : arr) {
System.out.print(num + " ");
}
}
}
def reverseInGroups(arr, k):
i = 0
n = len(arr)
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
if __name__ == "__main__":
arr = [1, 2, 3, 4, 5, 6, 7, 8]
k = 3
reverseInGroups(arr, k)
print(" ".join(map(str, arr)))
using System;
class GfG {
public static void reverseInGroups(int[] arr, int k){
int n = arr.Length;
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;
}
}
}
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;
reverseInGroups(arr, k);
for (int i = 0; i < n; i++) {
Console.Write(arr[i] + " ");
}
}
}
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);
console.log(arr1.join(" "));
Output
3 2 1 6 5 4 8 7