0

I'm trying to solve sum of to array problem:

//[1,2,3] + [1,2] should be [1,3,5]

I'm able to solve this if the array are the same size, but how can I deal with different array sizes? Here is my code for now:

function sumOfArrays(a, b) {

    let result = new Array(Math.max(a.length, b.length)); 
    let carry = 0;

    for (let i = result.length - 1; i >= 0; i--) {
        const elementA = a[i];
        const elementB = b[i];
        const additionResult = elementA + elementB + carry;
        result[i] = (additionResult % 10);
        carry = Math.floor(additionResult / 10);
    }
}

I'm basically getting null values into the result array If there is a difference in the size of the array

2
  • const elementA = a[i] || 0 Commented Jan 2, 2023 at 15:45
  • But for this to work you would need i to go from 1 to n and subtract it from the corresponding array length. Otherwise you pad right instead of left. Commented Jan 2, 2023 at 15:53

2 Answers 2

1

You could add a comparison if the 2 arrays are the same length.

If not, you can 'pad' it from the beginning with 0's until ther are the same length.

Then your code will work as expected (after adding return result ;) )

const pad = (arr, size, fill = 0) => [ ...Array(size - arr.length).fill(0), ...arr ];

let a = [1,2,3];
let b = [1,2];

if (a.length < b.length) {
    a = pad(a, b.length);
} else if (b.length < a.length) {
    b = pad(b, a.length);
}

function sumOfArrays(a, b) {

    let result = new Array(Math.max(a.length, b.length)); 
    let carry = 0;

    for (let i = result.length - 1; i >= 0; i--) {
        const elementA = a[i];
        const elementB = b[i];
        const additionResult = elementA + elementB + carry;
        result[i] = (additionResult % 10);
        carry = Math.floor(additionResult / 10);
    }
    
    return result;
}

const res = sumOfArrays(a, b);
console.log(res)


However, since the array's are now the same length, we can simplefy the code a lot by using map() and add (+) to current value of the other array on that index:

const pad = (arr, size, fill = 0) => [ ...Array(size - arr.length).fill(0), ...arr ];

let a = [1,2,3];
let b = [1,2];

if (a.length < b.length) {
    a = pad(a, b.length);
} else if (b.length < a.length) {
    b = pad(b, a.length);
}

function sumOfArrays(a, b) {
    return a.map((n, i) => n + b[i]);
}

const res = sumOfArrays(a, b);
console.log(res)
//  [
//    1,
//    3,
//    5
//  ]

Sign up to request clarification or add additional context in comments.

Comments

1

You could take an offset for the index to add.

function add(a, b) {
    const
        length = Math.max(a.length, b.length),
        offsetA = a.length - length,
        offsetB = b.length - length;

    return Array.from(
        { length },
        (_, i) => (a[i + offsetA] || 0) + (b[i + offsetB] || 0)
    );
}

console.log(...add([1, 2, 3], [1, 2])); // [1, 3, 5]
console.log(...add([1, 2, 3], [4, 5, 6])); 
console.log(...add([1, 2, 3, 4], [1])); // [1, 2, 3, 5]
console.log(...add([1], [1, 2, 3, 4])); // [1, 2, 3, 5]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.