-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTwoArray.cpp
More file actions
43 lines (34 loc) · 754 Bytes
/
Copy pathAddTwoArray.cpp
File metadata and controls
43 lines (34 loc) · 754 Bytes
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> addArray(vector<int> &arr, vector<int> brr)
{
vector<int> ans;
int carry = 0;
int i = arr.size() - 1, j = brr.size() - 1;
while (i >= 0 || j >= 0 || carry)
{
int n1 = (i>=0) ? arr[i] : 0;
int n2 = (j>=0) ? brr[j] : 0;
int sum = n1 + n2 + carry;
int digit = sum % 10;
carry = sum / 10;
ans.push_back(digit);
i--;
j--;
}
reverse(ans.begin(), ans.end());
return ans;
}
int main()
{
vector<int> nums = {9,9,9};
vector<int> nums1 = {1};
vector<int> ans = addArray(nums, nums1);
for (int val : ans)
{
cout << val;
}
return 0;
}