-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathGFG_AddTwoNumbersRepresentedByTwoArrays.cpp
79 lines (74 loc) · 1.52 KB
/
GFG_AddTwoNumbersRepresentedByTwoArrays.cpp
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
69
70
71
72
73
74
75
76
77
78
79
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution
{
public:
string calc_Sum(int *a, int n, int *b, int m)
{
int carry = 0;
string ans;
int i = n - 1, j = m - 1;
while (i >= 0 and j >= 0)
{
int x = a[i] + b[j] + carry;
int digit = x % 10;
carry = x / 10;
ans.push_back(digit + '0');
i--;
j--;
}
while (i >= 0)
{
int x = a[i] + 0 + carry;
int digit = x % 10;
carry = x / 10;
ans.push_back(digit + '0');
i--;
}
while (j >= 0)
{
int x = 0 + b[j] + carry;
int digit = x % 10;
carry = x / 10;
ans.push_back(digit + '0');
j--;
}
if (carry)
ans.push_back(carry + '0');
while (ans[ans.size() - 1] == '0')
{
ans.pop_back();
}
reverse(ans.begin(), ans.end());
return ans;
}
};
//{ Driver Code Starts.
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
int m;
cin >> m;
int b[m];
for (int i = 0; i < m; i++)
{
cin >> b[i];
}
Solution ob;
cout << ob.calc_Sum(a, n, b, m) << endl;
}
return 0;
}
// } Driver Code Ends