code is failed in the test case no 2 and 4.
please check it
Arrays-Sum Of Two Arrays Doubt
@Ajitverma1503
You have not worked for the condition when the resultant sum array could be larger than both the input arrays.
Input :
4
1 0 2 9
4
9 9 9 9
Expected Output :
1, 1, 0, 2, 8, END
Your Output :
1, 0, 2, 8, END
Didn’t able to think for the condition. Can you please Help ?
@Ajitverma1503
Make a bool flag variable , say
bool extraCarryOver = false;
This flag variable becomes true in case there is such a scenario of an extra carry over that the resultant array size is larger than either of the two input arrays.
It remains false otherwise.
We initialise it as false as I have above.
Now we only need to check whether there is an extra carry over and if it is , we make our flag true.
We check our carry over in condition
if( c ) {
}
You have already made this if in your code.
Rather than doing sum[k] = c in this if block , make our flag variable true
if( c ) {
extraCarryOver = true;
}
Now comes the printing part.
In case there is an extra carry over , we print the carry first. In case there was no carry over i.e. c = 0 , we skip this and move to printing the array in the loop.
if(extraCarryOver) {
cout << c << ", ";
}
This is followed by the loop printing the entire array as you have done in your code.
Make these changes and try.
Let me know if you need any further help.