Is the sum of two array elements wise?

There is a problem in which two random integer arrays are given in which numbers from 0 to 9 are present in each index (i.e., an integer integer is present in each index of both given arrays). I need to find the sum of the numbers represented by the input arrays and put the result in another array.

I believe that everything is in order with my code, since I execute it almost 50-60 times for different arrays. But when I submit it to my online school judge, he accepted only 4 test cases and rejected the other two. I can not understand in which case this will give the wrong result. Need a little help guys.

HERE IS MY CODE

public static int[] sumOfTwoArrays(int[] arr1, int[] arr2){ int size1 = arr1.length; int size2 = arr2.length; int carry = 0,sum,s,r; if(size1 == size2) { int arr3[] = new int[size1+1]; for(int i=arr1.length-1;i>=-1;i--) { if(i==-1) { arr3[i+1] = carry; //System.out.println(i+1+" "+arr3[i+1]); } else { sum = arr1[i] + arr2[i]; if(sum>9) { s =sum; r = s % 10; arr3[i+1] = carry + r; carry = 1; //System.out.println(i+" "+arr3[i]); } else { if(sum==9 && carry==1) { s =sum+carry; r = s % 10; arr3[i+1] = r; } else { arr3[i+1] = sum+carry; carry=0; } //System.out.println(i+" "+arr3[i]); } } } return arr3; } else if (size1>size2) { int arr3[] = new int[size1+1]; int diff = arr1.length - arr2.length; for(int i=arr1.length-1;i>=-1;i--) { if(i==-1) { arr3[i+1] = carry; } else { if(i>=diff) { sum = arr1[i] + arr2[i-diff]; if(sum>9) { s =sum; r = s % 10; arr3[i+1] = carry + r; carry = 1; } else { if(sum==9 && carry==1) { s =sum+carry; r = s % 10; arr3[i+1] = r; } else { arr3[i+1] = sum+carry; carry=0; } } } // end of diff i else { arr3[i+1] = arr1[i]; carry = 0; } } } return arr3; } else { int arr3[] = new int[size2+1]; int diff = arr2.length - arr1.length; for(int i=arr2.length-1;i>=-1;i--) { if(i==-1) { arr3[i+1] = carry; } else { if(i>=diff) { sum = arr2[i] + arr1[i-diff]; if(sum>9) { s =sum; r = s % 10; arr3[i+1] = carry + r; carry = 1; } else { if(sum==9 && carry==1) { s =sum+carry; r = s % 10; arr3[i+1] = r; } else { arr3[i+1] = sum+carry; carry=0; } } } // end of diff i else { arr3[i+1] = arr2[i]; carry = 0; } } } return arr3; } } 

Input Example:

 int[] arr1 = {8,5,3,9,6}; int[] arr2 = {3,3,3,3,3}; 

Output Example:

 {1,1,8,7,2,9} 

Input Example:

 int[] arr1 = {8,5,3,9,6}; int[] arr2 = {1,0,5}; 

Output Example:

 {0,8,5,5,0,1} 
+6
source share
4 answers

Well, I have this algorithm based on the Eran solution (it has fixed the error since it was fixed), I will share it, since I use less arrays.

 public static int[] sum(int[] arr1, int[] arr2){ int carry = 0; int sum = 0; int len1 = arr1.length; int len2 = arr2.length; int len = Math.max(len1, len2); int arr3[] = new int[len + 1]; for (int i = 1; i <= len; i++) { sum = (len1 - i >= 0 ? arr1[len1-i] : 0) + (len2 - i >= 0 ? arr2[len2-i] : 0) + carry; arr3[len-i+1] = sum%10; carry = sum/10; } arr3[0] = carry; return arr3; } 

Using the ternary operator is still readable, so I find this a good solution.

For a brief explanation, we read arrays from the end, using i to read from right to left, but based on the length of the arrays. Triple operation is used in case of different array sizes.

EDIT:

Your algorithm incorrectly manages the carry value with an array of different sizes.

185 + 16 gives 101 .

Just because you set values ​​like:

 arr3[i+1] = arr1[i]; 

So, you forgot the transfer that may occur in the last operation.

+4
source

This code is much more complicated than it should be, which increases the likelihood of it finding errors that are difficult to detect.

You do not need to execute the algorithm 3 times (based on whether the first array is smaller, larger, or equal to the second array). You can implement it once for two arrays of the same size, the size of which is Math.max(arr1.length,arr2.length) . This will eliminate 2/3 of your code.

 int len = Math.max(arr1.length,arr2.length); int[] arr11 = new int[len]; int[] arr22 = new int[len]; int arr3[] = new int[len+1]; for(int i=len-1;i>=-1;i--) { if (i>=len-arr1.length) arr11[i]=arr1[i-(len-arr1.length)]; if (i>=len-arr2.length) arr22[i]=arr2[i-(len-arr2.length)]; // now you can use arr11[i] and arr22[i] instead of arr1[i] and arr2[i] ... } 

In addition, instead of sum = arr1[i] + arr2[i]; I suggest you add the transfer immediately - sum = arr11[i] + arr22[i] + carry; . Now you only need to check once if there is sum > 9 .

  if(i==-1) { arr3[i+1] = carry; } else { sum = arr11[i] + arr22[i] + carry; if(sum>9) { arr3[i+1] = sum % 10; carry = 1; } else { arr3[i+1] = sum; carry = 0; } } 

Combining the two fragments, you get:

 int carry = 0; int sum = 0; int len = Math.max(arr1.length,arr2.length); int[] arr11 = new int[len]; int[] arr22 = new int[len]; int arr3[] = new int[len+1]; for(int i=len-1;i>=-1;i--) { if(i==-1) { arr3[i+1] = carry; } else { if (i>=len-arr1.length) arr11[i]=arr1[i-(len-arr1.length)]; if (i>=len-arr2.length) arr22[i]=arr2[i-(len-arr2.length)]; sum = arr11[i] + arr22[i] + carry; if(sum>9) { arr3[i+1] = sum % 10; carry = 1; } else { arr3[i+1] = sum; carry = 0; } } } return arr3; 

EDIT:

I had a small mistake. I added 0s in the least significant digits of the smaller array (which are high indices) instead of the most significant bits (low indices), which made the result incorrect if the arrays had different lengths. I fixed it, although now the part that copies the elements from the source arrays to arr11 and arr22 is less readable.

+2
source
 int[] firstArray = {1,8,8,8, 8}; int[] secondArray = {1,8,9}; String diffstring1 = "", diffstring2 = ""; for (int i = 0; i < firstArray.length; i++) { diffstring1 = diffstring1 + String.valueOf(firstArray[i]); } for (int i = 0; i < secondArray.length; i++) { diffstring2 = diffstring2 + String.valueOf(secondArray[i]); } int diff = Integer.parseInt(diffstring1) + Integer.parseInt(diffstring2); String diifffinal = String.valueOf(diff); int[] third = new int[diifffinal.length()]; for (int j = 0; j < diifffinal.length(); j++) { char abc = diifffinal.charAt(j); third[j] = Character.getNumericValue(abc); Log.d(TAG, "onCreate:---> " + third[j]); } 
0
source

If this leading 0 is not needed in your second output example, you can also use a different approach by first changing the input, for example. with function as follows:

 static Integer toNumber(int[] arr) { return Integer.valueOf(Arrays.stream(arr) .mapToObj(Integer::toString) .collect(Collectors.joining())); } 

This way you can just summarize your arrays as if they were normal integers:

 Integer sum = toNumber(arr1) + toNumber(arr2); 

Converting this back to an array can be done as follows:

 int[] sumArray = sum.toString().chars() .map(operand -> Character.digit(operand, 10)) .toArray(); 

But you do not have this leading 0 in your release. This code uses Java 8, but the same can be rewritten without streams (but unchecked):

 static Integer toNumber(int[] arr) { StringBuilder integerStrBuilder = new StringBuilder(); for (int i = 0; i < arr.length; i++) { integerStrBuilder.append(Integer.toString(arr[i])); } return Integer.valueOf(integerStrBuilder.toString()); } 

and for the array:

 char[] characters = sum.toString().toCharArray(); int[] sumArray = new int[characters.length]; for (int j = 0; j < characters.length; j++) { sumArray[j] = Characters.digit(characters[j], 10); } 
0
source

Source: https://habr.com/ru/post/1014612/


All Articles