Finding maximum number from two arrays in JavaScript


Problem

We are required to write a JavaScript function that takes in two arrays of single digit numbers representing two numbers, arr1 and arr2 as the first and second argument. The third argument to the function will be a number,

num (num <= length of arr1 + length of arr2)

Our function should return a new array of single digit numbers of length num, which in itself represents a number. And the number should be the maximum number we can create using element from both the arrays, the only condition for us is that we have to preserve the relative order of elements from the same array.

For example, if the input to the function is −

const arr1 = [1, 3, 4, 5, 6];
const arr2 = [9, 1, 2, 5, 8, 3];
const num = 4;

Then the output should be −

const output = [9, 8, 6, 3];

Example

The code for this will be −

const arr1 = [1, 3, 4, 5, 6];
const arr2 = [9, 1, 2, 5, 8, 3];
const num = 4;
const maxArray = (arr1 = [], arr2 = [], num) => {
   const map = new Map();
   const match = (a, b, num) => {
      if (map.has(a + ',' + b + ',' + num)) {
         return map.get(a + ',' + b + ',' + num);
      }
      let output = [];
      while(num > 0) {
         let maxa = -Infinity;
         let maxai = 0;
         let maxb = -Infinity;
         let maxbi = 0;
         for(let i = a; i < arr1.length && arr1.length + arr2.length - (i + b) >= num; i++) {
            if (arr1[i] > maxa) {
               maxa = arr1[i];
               maxai = i;
            }
         }
         for(let i = b; i < arr2.length && arr1.length + arr2.length - (a + i) >= num; i++) {
            if (arr2[i] > maxb) {
               maxb = arr2[i];
               maxbi = i;
            }
         }
         if (maxa === maxb) {
            output.push(maxa);
            let ca = map.get(a+','+(maxbi+1)+','+(num-1)) || match(a, maxbi+1, num-1);
            let cb = map.get((maxai+1)+','+b+','+(num-1)) || match(maxai+1,b,num-1);
            map.set(a+','+(maxbi+1)+','+(num-1), ca);
            map.set((maxai+1)+','+b+','+(num-1), cb);
            if (ca.join('') > cb.join('')) {
               return [...output, ...ca];
            } else {
               return [...output, ...cb];
            }
         } else if (maxa > maxb) {
            output.push(maxa);
            a = maxai + 1;
         } else {
            output.push(maxb);
            b = maxbi + 1;
         }
         num--;
      }
      map.set(a + ',' + b + ',' + num, output);
      return output;
   }
   return match(0, 0, num);
};
console.log(maxArray(arr1, arr2, num));

Code Explanation:

The steps we took are −

  • Used for-loop until remaining num is allowed.

  • If arr1 had a higher number than arr2, we used arr1 number first, otherwise we used arr2 number.

  • when arr1 and arr2 had the same number until the for-loop is allowed, we used just recursion to compare the two values, and chose the bigger number.

Output

And the output in the console will be −

[ 9, 8, 6, 3 ]

Updated on: 20-Mar-2021

460 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements