Problem of the day
You are given two numbers 'A' and 'B' in the form of two arrays (A[] and B[]) of lengths 'N' and 'M' respectively, where each array element represents a digit. You need to find the sum of these two numbers and return this sum in the form of an array.
Note:
1. The length of each array is greater than zero.
2. The first index of each array is the most significant digit of the number. For example, if the array A[] = {4, 5, 1}, then the integer represented by this array is 451 and array B[] = {3, 4, 5} so the sum will be 451 + 345 = 796. So you need to return {7, 9, 6}.
3. Both numbers do not have any leading zeros in them. And subsequently, the sum should not contain any leading zeros.
The first line of the input contains an integer T, denoting the number of test cases.
The first line of each test case contains two space-separated integers 'N' and 'M', denoting the size of the two arrays.
The second line of each test case contains 'N' space-separated integers denoting the elements of the first array.
The third line of each test case contains 'M' space-separated integers denoting the elements of the second array.
Output Format:
The only line of output of each test case contains space-separated digits which correspond to the sum of the two numbers 'A' and 'B'.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10^2
1 <= N, M <= 10^4
0 <= A[i], B[i] <= 9
Time Limit: 1 sec
2
4 1
1 2 3 4
6
3 2
1 2 3
9 9
1 2 4 0
2 2 2
For the first test case, the integer represented by the first array is 1234 and the second array is 6, so the sum is 1234 + 6 = 1240.
For the second test case, the integer represented by the first array is 123 and the second array is 99, so the sum is 123 + 99 = 222.
2
3 3
4 5 1
3 4 5
2 2
1 1
1 2
7 9 6
2 3
Can you think of using two-pointers?
O(max(N, M)), where N is the size of the first array and M is the size of the second array.
As we are iterating till we are not at the end of both the arrays, thus the time complexity will be linear towards the size of the arrays. Hence the time complexity will be O(max(N, M)).
O(1)
In this approach we are using constant extra space.