Javascript Program For Rearranging An Array In Maximum Minimum Form - Set 2 (O(1) extra space)
Last Updated :
30 Aug, 2024
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, 6}
Output: arr[] = {6, 1, 5, 2, 4, 3}
We have discussed a solution in below post:
Rearrange an array in maximum minimum form | Set 1 : The solution discussed here requires extra space, how to solve this problem with O(1) extra space.
In this post a solution that requires O(n) time and O(1) extra space is discussed. The idea is to use multiplication and modular trick to store two elements at an index.
even index : remaining maximum element.
odd index : remaining minimum element.
max_index : Index of remaining maximum element
(Moves from right to left)
min_index : Index of remaining minimum element
(Moves from left to right)
Initialize: max_index = 'n-1'
min_index = 0
// Can be any element which is more than
// the maximum value in array
max_element = arr[max_index] + 1
For i = 0 to n-1
If 'i' is even
arr[i] += arr[max_index] % max_element *
max_element
max_index--
// if 'i' is odd
ELSE
arr[i] += arr[min_index] % max_element *
max_element
min_index++
How does expression "arr[i] += arr[max_index] % max_element * max_element" work ?
The purpose of this expression is to store two elements at index arr[i]. arr[max_index] is stored as multiplier and "arr[i]" is stored as remainder. For example in {1 2 3 4 5 6 7 8 9}, max_element is 10 and we store 91 at index 0. With 91, we can get original element as 91%10 and new element as 91/10.
Below implementation of above idea:
JavaScript
// JavaScript program to rearrange an
// array in minimum maximum form
// Prints max at first position, min at
// second position second max at third
// position, second min at fourth
// position and so on.
function rearrange(arr, n) {
// Initialize index of first minimum and first
// maximum element
let max_idx = n - 1, min_idx = 0;
// Store maximum element of array
let max_elem = arr[n - 1] + 1;
// Traverse array elements
for (let i = 0; i < n; i++) {
// At even index : we have to put
// maximum element
if (i % 2 == 0) {
arr[i] += ((arr[max_idx] % max_elem) *
max_elem);
max_idx--;
}
// At odd index : we have to put
// minimum element
else {
arr[i] += ((arr[min_idx] % max_elem) *
max_elem);
min_idx++;
}
}
// Array elements back to it's
// original form
for (let i = 0; i < n; i++)
arr[i] = Math.floor(arr[i] / max_elem);
}
// Driver code
let arr = [1, 2, 3, 4, 5,
6, 7, 8, 9];
let n = arr.length;
console.log("Original Array");
for (let i = 0; i < n; i++)
console.log(arr[i]);
rearrange(arr, n);
console.log("Modified Array");
for (let i = 0; i < n; i++)
console.log(arr[i]);
// This code is contributed by Surbhi Tyagi.
OutputOriginal Array
1
2
3
4
5
6
7
8
9
Modified Array
9
1
8
2
7
3
6
4
5
Complexity Analysis:
- Time Complexity: O(N), as we are using a loop to traverse N times.
- Auxiliary Space: O(1), as we are not using any extra space.
Thanks Saurabh Srivastava and Gaurav Ahirwar for suggesting this approach.
Another Approach:
simpler approach will be to observe indexing positioning of maximum elements and minimum elements. The even index stores maximum elements and the odd index stores the minimum elements. With every increasing index, the maximum element decreases by one and the minimum element increases by one. A simple traversal can be done and arr[] can be filled in again.
Note: This approach is only valid when elements of given sorted array are consecutive i.e., vary by one unit.
Below is the implementation of the above approach:
JavaScript
// Javascript program to rearrange an
// array in minimum maximum form
// Prints max at first position, min
// at second position second max at
// third position, second min at
// fourth position and so on.
function rearrange(arr, n) {
// Initialize index of first minimum
// and first maximum element
let max_ele = arr[n - 1];
let min_ele = arr[0];
// Traverse array elements
for (let i = 0; i < n; i++) {
// At even index : we have to put
// maximum element
if (i % 2 == 0) {
arr[i] = max_ele;
max_ele -= 1;
}
// At odd index : we have to put
// minimum element
else {
arr[i] = min_ele;
min_ele += 1;
}
}
}
// Driver Code
let arr = [1, 2, 3, 4,
5, 6, 7, 8, 9];
let n = arr.length;
console.log("Original Array");
for (let i = 0; i < n; i++)
console.log(arr[i]);
rearrange(arr, n);
console.log("Modified Array");
for (let i = 0; i < n; i++)
console.log(arr[i]);
OutputOriginal Array
1
2
3
4
5
6
7
8
9
Modified Array
9
1
8
2
7
3
6
4
5
Complexity Analysis:
- Time Complexity: O(N), as we are using a loop to traverse N times.
- Auxiliary Space: O(1), as we are not using any extra space.
Please refer complete article on Rearrange an array in maximum minimum form | Set 2 (O(1) extra space) for more details!
Similar Reads
JavaScript Progam to rearrange an array in maximum minimum form using Two Pointer Technique 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 large
4 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
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 Queries to find maximum sum contiguous subarrays of given length in a rotating array Given an array arr[] of N integers and Q queries of the form {X, Y} of the following two types: If X = 1, rotate the given array to the left by Y positions.If X = 2, print the maximum sum subarray of length Y in the current state of the array. Examples:Â Input: N = 5, arr[] = {1, 2, 3, 4, 5}, Q = 2,
5 min read
Javascript Program For Sorting An Array Of 0s, 1s and 2s Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last.Examples:Input: {0, 1, 2, 0, 1, 2}Output: {0, 0, 1, 1, 2, 2}Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1}Output: {0, 0, 0, 0, 0
5 min read
Maximize the sum of modified values after rearranging given array based on condition array Given a binary array arr1[] and an integer array arr2[], each of length N, the task is to rearrange the elements in the array arr2 such that the total cost generated is maximized. The total cost generated is calculated by summation of modified values in the arr2 array. The values are modified in suc
7 min read