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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right