-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sumof2_modified.java
55 lines (52 loc) · 1.13 KB
/
Sumof2_modified.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package array_challenge;
import java.util.Scanner;
public class Sumof2_modified {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int m = s.nextInt();
int[] a1 = new int[m];
for (int i = 0; i < m; i++) {
a1[i] = s.nextInt();
}
int n = s.nextInt();
int[] a2 = new int[n];
for (int i = 0; i < n; i++) {
a2[i] = s.nextInt();
}
int max = 0;
if (m >= n)
max = m;
else
max = n;
int[] res = new int[max + 1];
int i = m - 1, j = n - 1, k = max;
while (i >= 0 && j >= 0) {
res[k] = res[k] + a1[i] + a2[j]; // you were reintializing it so it will not contain the carry
if (res[k] >= 10) {
res[k] = res[k] % 10;
res[k - 1] = res[k - 1] + 1;
}
k--;
j--;
i--;
}
while (i >= 0) {
res[k] += a1[i]; // same issue
i--;
k--;
}
while (j >= 0) {
res[k] += a2[j]; // same issue
j--;
k--;
}
if (res[0] == 1) {
System.out.print("1, ");
}
for (int x = 1; x < max + 1; x++) { // print
System.out.print(res[x] + ", ");
}
System.out.print("END");
}
}