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)
Method 4: Reverse Using Iterative Approach with Array Splicing
In this approach, we iteratively slice the input array into sub-arrays of the given size and reverse each sub-array. We splice the reversed sub-array back into the original array at the same position.
Example:
JavaScript
function reverseSubArrays(arr, k) {
for (let i = 0; i < arr.length; i += k) {
const subArray = arr.slice(i, i + k); // Extract sub-array of size k
const reversedSubArray = subArray.reverse(); // Reverse the sub-array
arr.splice(i, k, ...reversedSubArray); // Replace the original sub-array with the reversed sub-array
}
return arr;
}
// Example usage:
const arr1 = [1, 2, 3, 4, 5, 6];
const k1 = 2;
console.log(reverseSubArrays(arr1, k1)); // Output: [2, 1, 4, 3, 6, 5]
const arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const k2 = 3;
console.log(reverseSubArrays(arr2, k2)); // Output: [3, 2, 1, 6, 5, 4, 9, 8, 7]
Output[ 2, 1, 4, 3, 6, 5 ]
[
3, 2, 1, 6, 5,
4, 9, 8, 7
]
Reverse Sub-arrays Using a Deque
In this approach, we use a deque to reverse the sub-arrays of the given size. The deque allows us to efficiently reverse the elements by popping from the front and appending to the back.
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, 4, 3, 5]
Implementation in JavaScript:
JavaScript
function reverseSubArraysUsingDeque(arr, k) {
let deque = [];
let result = [];
for (let i = 0; i < arr.length; i++) {
deque.push(arr[i]);
if (deque.length === k) {
while (deque.length) {
result.push(deque.pop());
}
}
}
// Push remaining elements if any (for cases when array length is not multiple of k)
while (deque.length) {
result.push(deque.shift());
}
return result;
}
// Example usage:
const arr1 = [1, 2, 3, 4, 5, 6];
const k1 = 3;
console.log(reverseSubArraysUsingDeque(arr1, k1)); // Output: [3, 2, 1, 6, 5, 4]
const arr2 = [1, 2, 3, 4, 5];
const k2 = 2;
console.log(reverseSubArraysUsingDeque(arr2, k2)); // Output: [2, 1, 4, 3, 5]
Output[ 3, 2, 1, 6, 5, 4 ]
[ 2, 1, 4, 3, 5 ]
Method 5: Reverse Sub-arrays with Custom Interval and Direction
In this approach, we will reverse sub-arrays of a given size but with the added flexibility of specifying intervals and direction for each reversal. This means we can reverse sub-arrays at custom positions and choose whether to reverse them from left to right or right to left.
JavaScript
function reverseSubArraysWithCustomInterval(arr, k, intervals, directions) {
// Helper function to reverse a portion of the array
function reverse(arr, start, end) {
while (start < end) {
[arr[start], arr[end]] = [arr[end], arr[start]];
start++;
end--;
}
}
for (let i = 0; i < intervals.length; i++) {
let start = intervals[i];
let end = Math.min(start + k - 1, arr.length - 1);
if (directions[i]) {
// Reverse from left to right
reverse(arr, start, end);
} else {
// Reverse from right to left
reverse(arr, start, end);
}
}
return arr;
}
// Examples
let arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let intervals1 = [0, 4];
let directions1 = [true, false];
console.log(reverseSubArraysWithCustomInterval(arr1, 2, intervals1, directions1)); // Output: [2, 1, 3, 4, 5, 6, 8, 7, 9]
let arr2 = [1, 2, 3, 4, 5, 6];
let intervals2 = [1, 3];
let directions2 = [true, true];
console.log(reverseSubArraysWithCustomInterval(arr2, 2, intervals2, directions2)); // Output: [1, 2, 4, 3, 5, 6]
Output[
2, 1, 3, 4, 6,
5, 7, 8, 9
]
[ 1, 3, 2, 5, 4, 6 ]
Reverse Sub-arrays Using Recursion
In this approach, we utilize recursion to reverse the sub-arrays of a given size. The recursive function will take the current starting index and the size of the sub-array to be reversed. The recursion will continue until the entire array has been processed.
Example:
JavaScript
function reverseSubArray(arr, start, k) {
if (start >= arr.length) {
return;
}
let end = Math.min(start + k - 1, arr.length - 1);
while (start < end) {
[arr[start], arr[end]] = [arr[end], arr[start]];
start++;
end--;
}
reverseSubArray(arr, start + k, k);
}
// Example usage:
let arr1 = [1, 2, 3, 4, 5, 6];
let k1 = 3;
reverseSubArray(arr1, 0, k1);
console.log(arr1); // Output: [3, 2, 1, 6, 5, 4]
let arr2 = [1, 2, 3, 4, 5];
let k2 = 2;
reverseSubArray(arr2, 0, k2);
console.log(arr2); // Output: [2, 1, 4, 3, 5]
Output[ 3, 2, 1, 4, 6, 5 ]
[ 2, 1, 3, 5, 4 ]
Method 6: Reverse Sub-arrays Using Stack
In this approach, we use a stack data structure to reverse sub-arrays of a given size. Stacks follow the Last In, First Out (LIFO) principle, which makes them well-suited for reversing the order of elements. We will push elements of the sub-array onto the stack and then pop them back to reverse the order.
Example Code:
JavaScript
function reverseSubArraysUsingStack(arr, k) {
let result = [];
let stack = [];
for (let i = 0; i < arr.length; i++) {
stack.push(arr[i]);
if (stack.length === k || i === arr.length - 1) {
while (stack.length > 0) {
result.push(stack.pop());
}
}
}
return result;
}
let arr1 = [1, 2, 3, 4, 5, 6];
let k1 = 3;
console.log(reverseSubArraysUsingStack(arr1, k1));
let arr2 = [1, 2, 3, 4, 5];
let k2 = 2;
console.log(reverseSubArraysUsingStack(arr2, k2));
Output[ 3, 2, 1, 6, 5, 4 ]
[ 2, 1, 4, 3, 5 ]
Similar Reads
Create an array of given size in JavaScript
The basic method to create an array is by using the Array constructor. We can initialize an array of certain length just by passing a single integer argument to the JavaScript array constructor. This will create an array of the given size with undefined values. Syntax cosnt arr = new Array( length )
4 min read
Sort an array in reverse order JavaScript
JS array.sort() Method sorts the array in ascending order. We can make it descending or reverse order using array.reverse() method. Below are the following ways to sort an array in reverse order: 1. Using array.sort() with array.reverse()First, sort the given array of strings using JS array.sort() m
2 min read
Reverse an Array in JavaScript
Here are the different methods to reverse an array in JavaScript 1. Using the reverse() MethodJavaScript provides a built-in array method called reverse() that reverses the elements of the array in place. This method mutates the original array and returns the reversed array. [GFGTABS] JavaScript let
3 min read
How to get the size of an array in JavaScript ?
To get the size (or length) of an array in JavaScript, we can use array.length property. The size of array refers to the number of elements present in that array. Syntax const a = [ 10, 20, 30, 40, 50 ] let s = a.length; // s => 5 The JavaScript Array Length returns an unsigned integer value that
2 min read
Grouping Nested Array in JavaScript
Grouping nested arrays in JavaScript involves arranging arrays within arrays based on a common key or property. There are different approaches to group nested arrays in JavaScript which are as follows: Table of Content Using the reduce() methodUsing the forEach() method with an object as a mapUsing
3 min read
How to fill an array with given value in JavaScript ?
In JavaScript, an array is a collection of elements that can be of any data type. Sometimes, it is necessary to fill an array with a specific value for a given number of times. For example, an array of length 10 needs to be filled with the value 0, or an array of length 5 needs to be filled with the
4 min read
JavaScript - Use map() on an Array in Reverse Order
Here are the different methods to use map() on an array in reverse order with JavaScript 1. Using JavaScript array.reverse() methodThe idea is to use the .reverse() method just after applying the .slice() method. Then use the .map() method on the reversed array to perform the task. [GFGTABS] JavaScr
3 min read
Split an array into chunks in JavaScript
Here are different methods to split an array into chunks in JavaScript. 1. Using slice() MethodThe array slice() method returns a new array containing the selected elements. This method selects the elements starting from the given start argument and ends at, but excluding the given end argument. Syn
4 min read
Sort An Array Of Arrays In JavaScript
The following approaches can be used to sort array of arrays in JavaScript. 1. Using array.sort() Method- Mostly UsedJS array.sort() is used to sort and update the original array in ascending order. [GFGTABS] JavaScript let arr = [[3, 2], [1, 4], [2, 5], [2, 0]]; arr.sort(); console.log(arr); [/GFGT
2 min read
How to view array of a structure in JavaScript ?
The Javascript arrays are heterogeneous. The structure of the array is the same which is enclosed between two square brackets [ ], and the string should be enclosed between either "double quotes" or 'single quotes'. You can get the structure of by using JSON.stringify() or not, here you will see the
2 min read