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.
Below is the implementation of the above approach.
C++
// 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 elements 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 code int 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
// 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 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 code int 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
// Java program to reverse every sub-array // formed by consecutive k elements class GFG{ // Function to reverse every sub-array // formed by consecutive k elements static 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 code public 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 |
C#
// 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 static 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 code public 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 |
3 2 1 6 5 4 7
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Reverse an array in groups of given size
- Reverse an array in groups of given size | Set 2 (Variations of Set 1 )
- Reverse a Linked List in groups of given size (Iterative Approach)
- Move all zeroes to end of array | Set-2 (Using single traversal)
- Maximum difference between groups of size two
- Count all possible groups of size 2 or 3 that have sum as multiple of 3
- Minimum difference between groups of size two
- Find the size of largest group where groups are according to the xor of digits
- Maximize number of groups formed with size not smaller than its largest element
- Reverse zigzag Traversal of a Binary Tree
- Reverse Clockwise spiral traversal of a binary tree
- Reverse Level Order traversal in spiral form
- Reverse Anti Clockwise Spiral Traversal of a Binary Tree
- Minimum cost required to convert all Subarrays of size K to a single element
- Ways to split array into two groups of same XOR value
- Minimum sum obtained from groups of four elements from the given array
- Partition the array into two odd length groups with minimized absolute difference between their median
- Merge an array of size n into another array of size m+n
- Sort an array where a subarray of a sorted array is in reverse order
- Reduce the array to a single integer with the given operation
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

