-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_two_arrays.cpp
More file actions
68 lines (56 loc) · 1.06 KB
/
add_two_arrays.cpp
File metadata and controls
68 lines (56 loc) · 1.06 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
// add_two_arrays.cpp: Ashish Galagali
// Description: add two arrays
#include<iostream>
using namespace std;
int calSumUtil(int a[], int b[], int n, int m){
int sum[n];
int i = n-1;
int j = m-1;
int k = n-1;
int carry = 0, s = 0;
while (j>=0)
{
s= a[i] + b[j] +carry;
sum[k] = (s%10);
carry = s/10;
k--;
i--;
j--;
}
while (i >= 0)
{
s = a[i] + carry;
sum[k] = (s%10);
carry = s/10;
i--;
k--;
}
int ans =0;
if (carry)
{
ans = 10;
}
for (int i = 0; i < n-1; i++)
{
ans+= sum[i];
ans*=10;
}
return ans/10;
}
int calSum(int a[], int b[], int n, int m) {
if (n>=m)
{
return calSumUtil(a,b,n,m);
}
else
return calSumUtil(b,a,m,n);
}
int main()
{
int a[] = { 9, 3, 9 };
int b[] = { 6, 1 };
int n = sizeof(a) / sizeof(a[0]);
int m = sizeof(b) / sizeof(b[0]);
cout << calSum(a, b, n, m) << endl;
return 0;
}