Open In App

JavaScript Progam to rearrange an array in maximum minimum form using Two Pointer Technique

Last Updated : 11 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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(" "));


Output

Original 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);


Output

orignal array : [ 1, 2, 3, 4, 5, 6 ]
Modified array : [ 6, 1, 5, 2, 4, 3 ]



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads