Reverse an array in groups of given size | Set 2 (Variations of Set 1 )
Given an array, reverse every sub-array that satisfies the given constraints.
We have discussed a solution where we reverse every sub-array formed by consecutive k elements in Set 1. In this set, we will discuss various interesting variations of this problem.
Variation 1 (Reverse Alternate Groups):
Reverse every alternate 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, 4, 5, 6, 9, 8, 7] Input: arr = [1, 2, 3, 4, 5, 6, 7, 8] k = 2 Output: [2, 1, 3, 4, 6, 5, 7, 8]
Below is the implementation –
C++
// C++ program to reverse every alternate sub-array // formed by consecutive k elements #include <iostream> using namespace std; // Function to reverse every alternate sub-array // formed by consecutive k elements void reverse(int arr[], int n, int k) { // increment i in multiples of 2*k for (int i = 0; i < n; i += 2*k) { int left = i; // to handle case when 2*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() { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}; 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
// Java program to reverse // every alternate sub-array // formed by consecutive k elements class GFG { // Function to reverse every // alternate sub-array formed // by consecutive k elements static void reverse(int arr[], int n, int k) { // increment i in multiples of 2*k for (int i = 0; i < n; i += 2 * k) { int left = i; // to handle case when 2*k is not multiple of n int right = Math.min(i + k - 1, n - 1); // reverse the sub-array [left, right] while (left < right) { swap(arr, left++, right--); } } } static int[] swap(int[] array, int i, int j) { int temp = array[i]; array[i] = array[j]; array[j] = temp; return array; } // Driver code public static void main(String[] args) { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}; 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 has been contributed by 29AjayKumar |
Python3
# Python 3 program to reverse every alternate sub-array # formed by consecutive k elements # Function to reverse every alternate sub-array # formed by consecutive k elements def reverse(arr, n, k): # increment i in multiples of 2*k for i in range(0,n,2*k): left = i # to handle case when 2*k is not multiple of n right = min(i + k - 1, n - 1) # 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 if __name__ == '__main__': arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] k = 3 n = len(arr) reverse(arr, n, k) for i in range(0,n,1): print(arr[i],end =" ") # This code is contributed by # Surendra_Gangwar |
Output:
3 2 1 4 5 6 9 8 7 10 11 12 14 13
Variation 2 (Reverse at given distance):
Reverse every sub-array formed by consecutive k elements at given distance apart.
Examples:
Input: arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] k = 3 m = 2 Output: [3, 2, 1, 4, 5, 8, 7, 6, 9, 10] Input: arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] k = 3 m = 1 Output: [3, 2, 1, 4, 7, 6, 5, 8, 10, 9] Input: arr = [1, 2, 3, 4, 5, 6, 7, 8] k = 2 m = 0 Output: [2, 1, 4, 3, 6, 5, 8, 7]
Below is its implementation –
C++
// C++ program to reverse every sub-array formed by // consecutive k elements at given distance apart #include <iostream> using namespace std; // Function to reverse every sub-array formed by // consecutive k elements at m distance apart void reverse(int arr[], int n, int k, int m) { // increment i in multiples of k + m for (int i = 0; i < n; i += k + m) { int left = i; // to handle case when k + m 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() { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}; int k = 3; int m = 2; int n = sizeof(arr) / sizeof(arr[0]); reverse(arr, n, k, m); for (int i = 0; i < n; i++) cout << arr[i] << " "; return 0; } |
Java
// java program to reverse every sub-array formed by // consecutive k elements at given distance apart class GFG { // Function to reverse every sub-array formed by // consecutive k elements at m distance apart static void reverse(int[] arr, int n, int k, int m) { // increment i in multiples of k + m for (int i = 0; i < n; i += k + m) { int left = i; // to handle case when k + m is not multiple of n int right = Math.min(i + k - 1, n - 1); // reverse the sub-array [left, right] while (left < right) swap(arr,left++, right--); } } static int[] swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; return arr; } // Driver code public static void main(String[] args) { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}; int k = 3; int m = 2; int n = arr.length; reverse(arr, n, k, m ); for (int i = 0; i < n; i++) { System.out.print(arr[i] + " "); } } } // This code has been contributed by Rajput-Ji |
Output:
3 2 1 4 5 8 7 6 9 10 13 12 11 14
Variation 3 (Reverse by doubling the group size):
Reverse every sub-array formed by consecutive k elements where k doubles itself with every sub-array.
Examples:
Input: arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] k = 1 Output: [1], [3, 2], [7, 6, 5, 4], [15, 14, 13, 12, 11, 10, 9, 8]
Below is its C++ implementation –
// C++ program to reverse every sub-array formed by // consecutive k elements where k doubles itself with // every sub-array. #include <iostream> using namespace std; // Function to reverse every sub-array formed by // consecutive k elements where k doubles itself // with every sub-array. void reverse(int arr[], int n, int k) { // increment i in multiples of k where value // of k is doubled with each iteration for (int i = 0; i < n; i += k/2) { int left = i; // to handle case when number of elements in // last group is less than k int right = min(i + k - 1, n - 1); // reverse the sub-array [left, right] while (left < right) swap(arr[left++], arr[right--]); // double value of k with each iteration k = k*2; } } // Driver code int main() { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; int k = 1; int n = sizeof(arr) / sizeof(arr[0]); reverse(arr, n, k); for (int i = 0; i < n; i++) cout << arr[i] << " "; return 0; } |
Output:
1 3 2 7 6 5 4 15 14 13 12 11 10 9 8 16
Time complexity of all solutions discussed above 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 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Reverse an array in groups of given size
- Reverse a Linked List in groups of given size | Set 1
- Reverse a doubly linked list in groups of given size
- Minimum difference between groups of size two
- Count all possible groups of size 2 or 3 that have sum as multiple of 3
- Maximum difference between groups of size two
- Reverse a Linked List according to its Size
- Ways to split array into two groups of same XOR value
- Minimum sum obtained from groups of four elements from the given array
- 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
- Count number of permutation of an Array having no SubArray of size two or more from original Array
- Maximize the size of array by deleting exactly k sub-arrays to make array prime
- First string from the given array whose reverse is also present in the same array
- Perl | Reverse an array



