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. We will see the complete code to implement the concepts that will be discussed.

Introduction to Problem

In this problem, we are given two arrays and we have to make all the elements of the first array maximum as possible or simply we have to make the sum of all elements of the first array maximum. We can pick elements from the second array but the point is we have to pick only one element from the second array just once after that we can pick another element only. For example −

We are given two arrays −

Array1: 1 2 3 4 5 
Array2: 5 6 2 1 9

We can see there are many elements in the second array that are greater as compared to the elements which are present in the first array.

We can pick 9 in place of 3, 6 in place of 2, and 5 for 1. Which makes the final array looks like this −

5 6 9 4 5 

We will see two approaches both will come by sorting the arrays and two pointers but the only difference is where we will pick the pointers.

Approach

We have seen the example above, and from that, we can see that we can trade the small elements of the first array with the largest elements of the second array.

  • STEP 1 − First, we will sort both the arrays in increasing order and then we will reverse the second array to make it sorted in decreasing order.

  • STEP 2 − We will maintain two pointers that will point to the first index of both arrays.

  • STEP 3 − As the first element pointer will point toward the smallest number we can trade that number with the second array's largest number.

  • STEP 4 − At each iteration, we will swap both the array pointers and increase the pointers.

  • STEP 5 − If the elements of the first array's current index become greater as compared to the elements of the second array then we can stop further steps.

  • STEP 6 − At last, we will print the elements of the array.

Example

// 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()
   array2.sort()
   
   // reversing the arrays
   array1.reverse()
   array2.reverse()
   
   // 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){
         while(ptr3 != len1){
            ans[ptr3] = array1[ptr1];
            ptr3++;
            ptr1++;
         }
      }
      else if(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)
}
// declaring arrays
array1 = [1, 2, 4, 5, 3]
array2 = [5, 6, 2, 1, 9]

// calling the function
maximumArray(array1,array2)

Time and Space Complexity

The time complexity of the above code is O(N*log(N)) were N is the size of the given array and the log factor here is due to the sort function that we are using to sort the arrays.

We are using an extra array to store the elements which makes the space complexity as O(N), but that array is required to store the answer to it may or may not be considered as the extra space.

Direct Sorting Approach

In the previous approach, we were sorting the elements of the array and then we were using the two pointers approach, but there is a direct method with the help of which we can just do it simply −

  • By using the new keyword and Array keyword we will create a new array of the size of sum or lengths of both given array’s.

  • We will fill all the elements of both the given arrays into the new array one by one.

  • We will sort the newly created array to get the elements in the increasing order.

  • All the greatest elements are present in the end and we can get them easily.

Example

// function to find the maximum array
function maximumArray(array1, array2){
   var len1 = array1.length
   var len2 = array2.length
   var ans = new Array(len1+len2);
   for(var i = 0; i<len1; i++){
      ans[i] = array1[i];
   }
   for(var i = 0; i< len2; i++){
      ans[i+len1] = array2[i];
   }
   ans.sort();
   for(var i = 0;i<len1;i++){
      array1[i] = ans[len2+len1-i-1];
   }
   console.log("The final array is: ")
   console.log(array1)
}

// declaring arrays
array1 = [1, 2, 4, 5, 3]
array2 = [5, 6, 2, 1, 9]
// calling the function
maximumArray(array1,array2)

Time and Space Complexity

The time complexity of the above code is O(N*log(N)) were N is the size of the given array and the log factor here is due to the sort function that we are using to sort the arrays.

We are using an extra array to store the elements which make the space complexity O(N).

Conclusion

In the above tutorial, we have implemented 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. We have seen two methods both use the concept of sorting. One method with two pointers takes the time of O(N*log(N)) and space of O(1), while other method takes the same time but O(N) space.

Updated on: 30-Mar-2023

110 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements