Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sumoftwoarray.cpp #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
sumoftwoarray.cpp
PerksofbeingVaibhav authored Oct 26, 2022
commit ea21caeed315e60d56e80f33137961faa80353a1
79 changes: 79 additions & 0 deletions sumoftwoarray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <iostream>
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;
}