-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day-041.cpp
66 lines (60 loc) · 1.26 KB
/
Day-041.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
#include<iostream>
#include<vector>
using namespace std;
vector<int> reverse(vector<int> v){
int s = 0;
int e = v.size()-1;
while(s<e){
swap(v[s++],v[e--]);
}
return v;
}
vector<int> findArraySum(vector<int>&a, int n, vector<int>&b, int m) {
int i = n-1;
int j = m-1;
vector<int> ans;
int carry = 0;
while(i>=0 && j>=0){
int val1 = a[i];
int val2 = b[j];
int sum = val1 + val2 + carry;
carry = sum/10 ;
sum = sum%10;
ans.push_back(sum);
i--;
j--;
}
//First case
while(i>=0){
int sum = a[i] + carry;
carry = sum /10;
sum = sum % 10;
ans.push_back(sum);
i--;
}
//Second case
while(j>=0){
int sum = b[j] + carry;
carry = sum /10;
sum = sum % 10;
ans.push_back(sum);
j--;
}
// Last case
while(carry != 0){
int sum = carry;
carry = sum/10;
sum = sum % 10;
ans.push_back(sum);
}
return reverse(ans);
}
int main (){
vector<int> arr1{2,3,4};
vector <int> arr2{5,6,7,8},temp;
temp=findArraySum(arr1,3,arr2,4);
for(int i=0;i<temp.size();i++){
cout<<temp.at(i)<<endl;
}
return 0;
}