HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Sum Of Two Arrays element by element and arrays can be of unequal length

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
canarrayslengthtwoelementsumandunequal

Problem

In this question, I need to find the sum of 2 arrays element by element.
Arrays can be of unequal length.

Explanation: Sum of [1, 0, 2, 9] and [3, 4, 5, 6, 7] is [3, 5, 5, 9, 6] and the first digit represents carry over , if any (0 here ).

Here's what I could come up with. Let me know if anything could be done better,or anything i did wrong.

Also earlier someone suggested me that I should split my code into smaller pieces that are easier to read and test individually. Perhaps if anyone could help with the method signatures that I should make(if any).

```
private static void sum(int[] arr1, int[] arr2) {
ArrayList arraySum = new ArrayList<>();
int[] largerArray, smallerArray;
boolean flag;
if (arr1.length == arr2.length) {
largerArray = arr1;
smallerArray = arr2;
flag = true;
} else if (arr1.length > arr2.length) {
largerArray = arr1;
smallerArray = arr2;
flag = false;
} else {
largerArray = arr2;
smallerArray = arr1;
flag = false;
}
int sum = 0, carry = 0;
int diff = largerArray.length - smallerArray.length;
for (int i = smallerArray.length - 1; i >= 0; i--) { //traverse through smaller array
sum = largerArray[i + diff] + smallerArray[i] + carry;
carry = 0;
if (sum > 9) {
int N = sum;
while (N != 0) {
int rem = N % 10;
if (N = 0; i--) { // now sum remaining elements of larger array
sum = arr1[i] + carry;
carry = 0;
if (sum > 9) {
int N = sum;
while (N != 0) {
int rem = N % 10;
if (N < 10) {
carry = rem;
} else {
arraySum.add(rem);
}
if (i == 0 && carry != 0) { //if we are at last element & carry!=0 add carry as first digit

Solution

Instead of reviewing your code as is, I'll propose an alternative solution that should be a tad simpler than yours:

We don't need to know which array of the two is the larger, we only need our result array to be able to contain it:

int[] result = new int[Math.max(arr1.length, arr2.length) + 1];


Note that we need an extra element to adjust for possible "overflow" (aka. carry in the last added digit).

Now since we need to sum things, let's just copy over one of the two arrays into our result:

System.arraycopy(arr1, 0, result, result.length - arr1.length, arr1.length);


Now the only thing that remains to be done is doing the addition with the other array. We'll need something to store the carry in. Luckily by nature of a carry, it's maximum value is always 1 (it seems you didn't notice that). So we either have a carry-flag or not:

boolean carry = false;


Now let's walk the other array, and perform the addition. Note that I use the conventional iteration indexing \$\{1,\ldots,length-1\}\$ instead of the backwards indexing you used to simplify the reasoning in the addition:

for (int i = 0; i = 10) {
        carry = true;
        digitResult -= 10;
    } else {
        carry = false;
    }
    result[result.length - 1  - i] = digitResult;
}


This was the "interesting part" already. The only thing remaining to be done is cleaning up a possibly remaining carry:

int i = arr2.length;
while (carry) {
    int r = result[result.length - 1 - i] + 1;
    if (r >= 10) { // could also be == 10
        r -= 10;
    } else {
        carry = false;
    }
    result[result.length - 1 - i] = r;
    i++;
}


Theoretically we might need to check the array access, but it's mathematically provable we don't need to, so I won't do checks. An explanatory comment could help for doing that.

Now as Timothy Truckle already mentioned in his answer your method is responsible for too many things. You should at this point just return the result array to the caller, and that's the extent of the method.

There's some repetition and "uncleanliness" in this code, that I left in on purpose. I'm pretty sure with some thinking you should be able to clean the code up a bit, and it's hard to learn it just from a person telling you what's wrong. Find out :)

Code Snippets

int[] result = new int[Math.max(arr1.length, arr2.length) + 1];
System.arraycopy(arr1, 0, result, result.length - arr1.length, arr1.length);
boolean carry = false;
for (int i = 0; i < arr2.length; i++) {
    int digitResult = result[result.length - 1 - i] + arr2[arr2.length - 1 - i];
    // usually you'd write this on properly separate lines
    if (carry) { digitResult += 1; }
    if (digitResult >= 10) {
        carry = true;
        digitResult -= 10;
    } else {
        carry = false;
    }
    result[result.length - 1  - i] = digitResult;
}
int i = arr2.length;
while (carry) {
    int r = result[result.length - 1 - i] + 1;
    if (r >= 10) { // could also be == 10
        r -= 10;
    } else {
        carry = false;
    }
    result[result.length - 1 - i] = r;
    i++;
}

Context

StackExchange Code Review Q#153830, answer score: 2

Revisions (0)

No revisions yet.