Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program for Maximize Elements Using Another Array
In this article, we are going to implement a JavaScript program for maximizing elements using another array. We are given two arrays and have to pick some elements from the second array and replace the elements of the first array to maximize the sum.
Problem Statement
In this problem, we are given two arrays and we have to make all the elements of the first array as large as possible by replacing them with elements from the second array. Each element from the second array can only be used once. The goal is to maximize the sum of the first array.
For example, given two arrays:
Array1: [1, 2, 3, 4, 5] Array2: [5, 6, 2, 1, 9]
We can replace smaller elements from array1 with larger elements from array2. The optimal result would be:
Final Array: [5, 6, 9, 4, 5]
Using Two Pointers Approach
The strategy is to sort both arrays and use two pointers to pick the best elements. We sort the first array in ascending order and the second array in descending order, then greedily select the maximum available element at each position.
Algorithm Steps
- STEP 1: Sort the first array in ascending order and the second array in descending order
- STEP 2: Use two pointers to traverse both arrays
- STEP 3: At each position, choose the larger element between the current elements of both arrays
- STEP 4: Continue until we've filled all positions in the result array
// function to find the maximum array
function maximumArray(array1, array2) {
var len1 = array1.length;
var len2 = array2.length;
// sorting the elements of both arrays
array1.sort((a, b) => a - b);
array2.sort((a, b) => b - a);
// traversing over the arrays
var ptr1 = 0;
var ptr2 = 0;
var ptr3 = 0;
// creating new array to store the answer
var ans = new Array(len1);
while (ptr3 < len1) {
if (ptr2 == len2) {
// no more elements in array2, use remaining from array1
while (ptr3 != len1) {
ans[ptr3] = array1[ptr1];
ptr3++;
ptr1++;
}
} else if (ptr1 < len1 && array1[ptr1] > array2[ptr2]) {
ans[ptr3] = array1[ptr1];
ptr1++;
} else {
ans[ptr3] = array2[ptr2];
ptr2++;
}
ptr3++;
}
console.log("The final array is:");
console.log(ans);
return ans;
}
// declaring arrays
var array1 = [1, 2, 4, 5, 3];
var array2 = [5, 6, 2, 1, 9];
// calling the function
maximumArray(array1, array2);
The final array is: [9, 6, 5, 5, 4]
Direct Sorting Approach
A simpler approach is to combine both arrays, sort them in descending order, and take the largest elements equal to the size of the first array.
// function to find the maximum array
function maximumArray(array1, array2) {
var len1 = array1.length;
var len2 = array2.length;
// combine both arrays
var combined = new Array(len1 + len2);
for (var i = 0; i < len1; i++) {
combined[i] = array1[i];
}
for (var i = 0; i < len2; i++) {
combined[i + len1] = array2[i];
}
// sort in descending order
combined.sort((a, b) => b - a);
// take the largest len1 elements
var result = [];
for (var i = 0; i < len1; i++) {
result[i] = combined[i];
}
console.log("The final array is:");
console.log(result);
return result;
}
// declaring arrays
var array1 = [1, 2, 4, 5, 3];
var array2 = [5, 6, 2, 1, 9];
// calling the function
maximumArray(array1, array2);
The final array is: [9, 6, 5, 5, 4]
Comparison
| Approach | Time Complexity | Space Complexity | Advantages |
|---|---|---|---|
| Two Pointers | O(N log N) | O(N) | More memory efficient for large arrays |
| Direct Sorting | O(N log N) | O(N) | Simpler implementation |
Conclusion
Both approaches effectively solve the problem of maximizing array elements using another array. The direct sorting method is simpler to implement, while the two-pointers approach offers better understanding of the underlying logic. Both have the same time complexity of O(N log N) due to sorting operations.
