JavaScript Progam to rearrange an array in maximum minimum form using Two Pointer Technique
Last Updated :
26 Aug, 2024
In this article, we are going to learn about rearranging an array in maximum minimum form using the Pointer Technique in JavaScript. Rearranging an array in maximum-minimum form with the Two Pointer Technique involves sorting the array, then alternately selecting elements from the smallest and largest ends, creating a new array with maximum-minimum ordering.
Example:
Input:
arr[] = {1, 2, 3, 4, 5, 6, 7}
Output:
arr[] = {7, 1, 6, 2, 5, 3, 4}
Input:
arr[] = {1, 2, 3, 4, 5, 6}
Output:
arr[] = {6, 1, 5, 2, 4, 3}
There are several methods that can be used to Rearrange an array in maximum minimum form using the Two Pointer Technique in JavaScript, which is listed below:
We will explore all the above methods along with their basic implementation with the help of examples.
Using an auxiliary array
The approach uses an auxiliary array to temporarily store modified elements. It employs 'small' and 'large' pointers tracking the smallest and largest values in the original array. A 'flag' toggles between placing the next element from 'small' or 'large'. The original array is iterated, filling the modified array accordingly and updating the small and large pointers. Finally, the modified array is copied back to the original array to achieve the desired rearrangement.
Syntax
const temp = new Array(n);
Example : In this example,the rearrange function creates a new array by alternately selecting elements from the smallest and largest ends of the original array. It modifies arr in-place, creating a maximum-minimum rearranged array.
JavaScript
function rearrange(arr) {
const n = arr.length;
const temp = new Array(n);
let small = 0;
let large = n - 1;
let flag = true;
for (let i = 0; i < n; i++) {
if (flag) {
temp[i] = arr[large--];
} else {
temp[i] = arr[small++];
}
flag = !flag;
}
for (let i = 0; i < n; i++) {
arr[i] = temp[i];
}
}
const arr = [1, 2, 3, 4, 5, 6];
console.log("Original Array");
console.log(arr.join(" "));
rearrange(arr);
console.log("\nModified Array");
console.log(arr.join(" "));
OutputOriginal Array
1 2 3 4 5 6
Modified Array
6 1 5 2 4 3
Using sort() method
In this approach,we perform sorting an array in ascending order and then rearranging it. It uses two pointers, one starting from the beginning and the other from the end, to create a new array in maximum-minimum form.
Syntax
arr.sort(compareFunction);
Example: In this example the rearrange function sorts an array in ascending order and then creates a new array in maximum-minimum form using two pointers. It's applied to arr1, and both the original and modified arrays are displayed in the console.
JavaScript
function rearrange(arr) {
arr.sort((a, b) => a - b);
const n = arr.length;
const result = [];
for (let left = 0, right = n - 1;
left <= right; left++,
right--) {
result.push(arr[right]);
if (left !== right) {
result.push(arr[left]);
}
}
return result;
}
const arr1 = [1, 2, 3, 4, 5, 6];
const result = rearrange(arr1);
console.log("orignal array :", arr1)
console.log("Modified array :", result);
Outputorignal array : [ 1, 2, 3, 4, 5, 6 ]
Modified array : [ 6, 1, 5, 2, 4, 3 ]
In-Place Rearrangement with Two Pointers
Idea:
- Sort the array: Start by sorting the array in ascending order.
- Use two pointers: One pointer starts from the beginning (
small
), and the other pointer starts from the end (large
) of the sorted array. - Modify in place: Use a temporary variable to store values during the rearrangement to avoid overwriting data.
Steps:
- Sort the Array: First, sort the array in ascending order.
- Use Two Pointers:
- Initialize one pointer (
small
) at the beginning and the other pointer (large
) at the end of the sorted array. - Alternate between the two pointers, placing the elements from the end (largest) and then from the beginning (smallest) into the appropriate positions.
- Directly Modify the Original Array: Using a temporary variable, place the largest and smallest values alternately in the original array.
Example:
JavaScript
function rearrange(arr) {
const n = arr.length;
arr.sort((a, b) => a - b);
let small = 0;
let large = n - 1;
let temp = arr.slice();
for (let i = 0; i < n; i++) {
if (i % 2 === 0) {
arr[i] = temp[large--];
} else {
arr[i] = temp[small++];
}
}
}
const arr = [1, 2, 3, 4, 5, 6];
console.log("Original Array");
console.log(arr.join(" "));
rearrange(arr);
console.log("\nModified Array");
console.log(arr.join(" "));
OutputOriginal Array
1 2 3 4 5 6
Modified Array
6 1 5 2 4 3
Similar Reads
Rearrange an array in maximum minimum form using Two Pointer Technique
Given a sorted array of positive integers, rearrange the array alternately i.e first element should be a maximum value, at second position minimum value, at third position second max, at fourth position second min, and so on. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: arr[] = {7, 1, 6, 2
6 min read
Javascript Program For Rearranging An Array In Maximum Minimum Form - Set 2 (O(1) extra space)
Given a sorted array of positive integers, rearrange the array alternately i.e first element should be the maximum value, second minimum value, third-second max, fourth-second min and so on. Examples:Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: arr[] = {7, 1, 6, 2, 5, 3, 4}Input: arr[] = {1, 2, 3, 4
5 min read
Javascript Program to Rearrange positive and negative numbers in O(n) time and O(1) extra space
An array contains both positive and negative numbers in random order. Rearrange the array elements so that positive and negative numbers are placed alternatively. Number of positive and negative numbers need not be equal. If there are more positive numbers they appear at the end of the array. If the
3 min read
Maximum and minimum of an array using minimum number of comparisons
Given an array of size N. The task is to find the maximum and the minimum element of the array using the minimum number of comparisons.Examples:Input: arr[] = {3, 5, 4, 1, 9}Output: Minimum element is: 1 Maximum element is: 9Input: arr[] = {22, 14, 8, 17, 35, 3}Output: Minimum element is: 3 Maximum
15+ min read
Maximize minimum element of an Array using operations
Given an array A[] of N integers and two integers X and Y (X ⤠Y), the task is to find the maximum possible value of the minimum element in an array A[] of N integers by adding X to one element and subtracting Y from another element any number of times, where X ⤠Y. Examples: Input: N= 3, A[] = {1,
8 min read
Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
Given an array arr[] of integers, the task is to find the minimum number greater than the maximum element from the array which cannot be formed using the numbers in the array (adding elements to form some other number). If no such element exists then print -1. Examples: Input: arr[] = {2, 6, 9} Outp
9 min read
Minimize product of maximum numbers in two Array using swaps | Set 2
Given two arrays arr1[] and arr2[] of N size each. Select any index i and swap the element arr1[i] and arr2[i]. By applying this operation any number of times(possibly none) find the minimum possible product of maximum elements from array arr1[] and arr2[]. Examples: Input: N = 6arr1[] = {1, 2, 6, 5
6 min read
Minimum cost to equal all elements of array using two operation
Given an array arr[] of n positive integers. There are two operations allowed: Operation 1 : Pick any two indexes, increase value at one index by 1 and decrease value at another index by 1. It will cost a. Operation 2 : Pick any index and increase its value by 1. It will cost b. The task is to find
8 min read
Minimize product of maximum numbers in two Array using swaps
Given 2 arrays arr1[] and arr2[] of size N the task is to find the minimum product of maximums of both arrays by performing a swapping operation any number of times. In one swap operation, any index of the array is selected and arr1[index] and arr2[index] are swapped. Examples: Input: N = 2, arr1[2]
4 min read
Program to reverse an array using pointers
Prerequisite : Pointers in C/C++ Given an array, write a program to reverse it using pointers . In this program we make use of * operator . The * (asterisk) operator denotes the value of variable . The * operator at the time of declaration denotes that this is a pointer, otherwise it denotes the val
4 min read