-
Notifications
You must be signed in to change notification settings - Fork 0
/
SumofTwoArrays.cpp
102 lines (83 loc) · 2.28 KB
/
SumofTwoArrays.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Two random integer arrays/lists have been given as ARR1 and ARR2 of size N and M respectively. Both the arrays/lists contain numbers from 0 to 9(i.e. single digit integer is present at every index). The idea here is to represent each array/list as an integer in itself of digits N and M.
// You need to find the sum of both the input arrays/list treating them as two integers and put the result in another array/list i.e. output array/list will also contain only single digit at every index.
// Note:
// The sizes N and M can be different.
// Output array/list(of all 0s) has been provided as a function argument. Its size will always be one more than the size of the bigger array/list. Place 0 at the 0th index if there is no carry.
// No need to print the elements of the output array/list.
// Using the function "sumOfTwoArrays", write the solution to the problem and store the answer inside this output array/list. The main code will handle the printing of the output on its own.
// Sample Input 1:
// 1
// 3
// 6 2 4
// 3
// 7 5 6
// Sample Output 1:
// 1 3 8 0
// My Code:
void sumOfTwoArrays(int *input1, int size1, int *input2, int size2, int *output)
{
int i = size1-1, j = size2-1;
int carry=0;
int k;
if(size1 < size2){
k = size2;
}
else{
k = size1;
}
while(k >= 0){
output[k] = (input1[i]+input2[j]+carry)%10;
carry = (input1[i]+input2[j]+carry)/10;
if(i>0)
i--;
else{
i=-1;
input1[i] = 0;
}
if(j>0)
j--;
else{
j=-1;
input2[j] = 0;
}
k--;
}
}
// Main Code:
#include <iostream>
using namespace std;
#include "solution.h"
int main()
{
int t;
cin >> t;
while (t--)
{
int size1;
cin >> size1;
int *input1 = new int[size1];
for (int i = 0; i < size1; ++i)
{
cin >> input1[i];
}
int size2;
cin >> size2;
int *input2 = new int[size2];
for (int i = 0; i < size2; ++i)
{
cin >> input2[i];
}
int outsize = 1 + max(size1, size2);
int *output = new int[outsize];
sumOfTwoArrays(input1, size1, input2, size2, output);
for (int i = 0; i < outsize; ++i)
{
cout << output[i] << " ";
}
delete[] input1;
delete[] input2;
delete[] output;
cout << endl;
}
return 0;
}