From ea21caeed315e60d56e80f33137961faa80353a1 Mon Sep 17 00:00:00 2001 From: Vaibhav Chaudhary <83056597+PerksofbeingVaibhav@users.noreply.github.com> Date: Wed, 26 Oct 2022 11:52:47 +0530 Subject: [PATCH] sumoftwoarray.cpp --- sumoftwoarray.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 sumoftwoarray.cpp diff --git a/sumoftwoarray.cpp b/sumoftwoarray.cpp new file mode 100644 index 0000000..c2d7275 --- /dev/null +++ b/sumoftwoarray.cpp @@ -0,0 +1,79 @@ +#include +using namespace std; + +void sumOfTwoArrays(int *input1, int size1, int *input2, int size2, int *output) +{ + int n=max(size1, size2); + int i=size1-1; + int j=size2-1; + int carry=0; + + while(i>=0 && j>=0) { + int sum=input1[i--]+input2[j--]+carry; + output[n--]=sum%10; + carry=sum/10; + } + + while(i>=0) { + int sum=input1[i--]+carry; + output[n--]=sum%10; + carry=sum/10; + } + + while(j>=0) { + int sum=input2[j--]+carry; + output[n--]=sum%10; + carry=sum/10; + } + + if(n>=0) { + output[n--]=carry; + } +} + +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; +}