I am trying to solve this problem using the an extra error. I am not getting the logic when we have have the arrays of different size means if we add arr[n-1] + arr2[m-1] so I need to solve the sum at the last index in resultant array what would be index.
Two arrays problems
Hey
Let me walk you through a very easy approach for solving this problem
Suppose the arrays given to you are
a[] = [1, 3, 5, 7, 9]
b[] = [2, 4, 6]
Now these have different sizes too
What we do is we convert this array into a number
like a becomes integer 13579
and b becomes integer 246
you add these numbers, ans becomes 13825
you convert this to array again as [1,3,8,2,5]
Now you can easily convert a number to an array,
Let me tell you how to convert array to a number
like for array [2,4,6]
i initialise a variable int num = 0;
and a multiplier int p = 1;
i iterate from right side
for(int i = n-1; i>=0; i–){
num+=(parr[i])
p = p10;
}
via this loop addition becomes 6+ (410) + (2100)
which makes is 246
@mahimahans111 do u think its a feasible solution when we have very arrays it num is int it might no able to store all that digits
Hey
You can follow the following approach as well-:
The idea is to start traversing both the array simultaneously from the end until we reach the 0th index of either of the array. While traversing each elements of array, add element of both the array and carry from the previous sum. Now store the unit digit of the sum and forward carry for the next index sum. While adding 0th index element if the carry left, then append it to beginning of the number.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.