Open In App

Reverse an Array in Groups of Given Size in JavaScript

Last Updated : 29 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to reverse a sub-array of a given size. We will be provided with an array and a random size for the sub-array to be reversed. There exist different variations of this problem.

These are the following variations of reversing the array in groups of a given size:

Reverse the alternate groups

In this variation of reversing the array, we will reverse the alternate sub-array of the given number of elements. It means the sub-arrays of the given size will be reversed from start and end, while the middle elements will remain at the same positions.

Example:

Input: arr = [1, 2, 3, 4, 5, 6], k=3
Output: [3, 2, 1, 6, 5, 4]

Input: arr = [1, 2, 3, 4, 5], k=2
Output: [2, 1, 3, 5, 4]

Example: The below example will explain the practical implementation of this variation in JavaScript:

Javascript




function groupReverse(arr, n, k) {
    let leftStart = 0, leftEnd = k - 1;
    let rightStart = n - k, rightEnd = n - 1;
    while (leftStart < leftEnd) {
        let temp = arr[leftStart];
        arr[leftStart] = arr[leftEnd];
        arr[leftEnd] = temp;
        leftStart++; leftEnd--;
    }
    while (rightStart < rightEnd) {
        let temp = arr[rightStart];
        arr[rightStart] = arr[rightEnd];
        arr[rightEnd] = temp;
        rightStart++; rightEnd--;
    }
    console.log(arr);
}
 
let array = [1, 2, 3, 4, 5];
let array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
groupReverse(array, 5, 3);
groupReverse(array2, 9, 3);


Output

[ 3, 2, 5, 4, 1 ]
[
  3, 2, 1, 4, 5,
  6, 9, 8, 7
]

Time Complexity: O(k), k is the size of the sub-array

Space Complexity: O(1)

Reverse at a given distance

In this variation, we will be provided with two integer type variables k and m. Where, k will be the size of the sub-array and m will be the distance between the sub-arrays. Hence, we have to reverse the sub-arrays of size k after a distance of m elements between them.

Example:

Input: arr = [1, 2, 3, 4, 5, 6], k = 2, m = 2
Output: [2, 1, 3, 4, 6, 5]

Input: arr = [1, 2, 3, 4, 5, 6, 7, 8, 9], k = 3, m = 3
Output:
[3, 2, 1, 4, 5, 6, 9, 8, 7]

Example: The below example will explain you the practical implementation in JavaScript.

Javascript




function groupReverse(arr, n, k, m) {
    let i = 0;
    while (i < n) {
        let leftStart = i;
        let leftEnd = Math.min(i + k - 1,
            n - 1);
 
        while (leftStart < leftEnd) {
            let temp = arr[leftStart];
            arr[leftStart] = arr[leftEnd];
            arr[leftEnd] = temp;
            leftStart++; leftEnd--;
        }
        i += k + m;
    }
    console.log(arr);
}
 
let array = [1, 2, 3, 4, 5, 6];
let array2 = [1, 2, 3, 4, 5,
    6, 7, 8];
groupReverse(array, 6, 2, 2);
groupReverse(array2, 8, 2, 4);


Output

[ 2, 1, 3, 4, 6, 5 ]
[
  2, 1, 3, 4,
  5, 6, 8, 7
]

Time Complexity: O(n), n is the size of array.

Space Complexity: O(1)

Reverse by doubling the group size

In this variation, the size of the sub-array will be doubled everytime and the sub-array of the new size will be reversed.

Example:

Input: arr = [1, 2, 3, 4, 5, 6, 7], k = 1
Output: [ [1], [3, 2], [7, 6, 5, 4] ]

Input:
arr = [1, 2, 3, 4, 5, 6], k = 2
Output: [ [2, 1], [6, 5, 4, 3] ]

Example: Below example will illustrate the implementation of this variation in JavaScript.

Javascript




function groupReverse(arr, n, k) {
    let i = 0;
    while (i < n) {
        let leftStart = i;
        let leftEnd = Math.min(i + k - 1,
            n - 1);
 
        while (leftStart < leftEnd) {
            let temp = arr[leftStart];
            arr[leftStart] = arr[leftEnd];
            arr[leftEnd] = temp;
            leftStart++; leftEnd--;
        }
        k *= 2;
        i += k / 2;
    }
    console.log(arr);
}
 
let array = [1, 2, 3, 4, 5, 6];
let array2 = [1, 2, 3, 4, 5, 6, 7,
    8, 9, 10, 11, 12, 13, 14];
groupReverse(array, 6, 2);
groupReverse(array2, 14, 3);


Output

[ 2, 1, 6, 5, 4, 3 ]
[
   3,  2, 1,  9,  8,  7,
   6,  5, 4, 14, 13, 12,
  11, 10
]

Time Complexity: O(n), n is the size of array

Space Complexity: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads