Sum of Two Numbers Represented as Arrays

Given two numbers in the form of two arrays where each element of the array represents a digit, calculate the sum of these two numbers and return this sum as an array.

Explanation:

You are provided with two arrays, A[] and B[], representing two numbers with lengths 'N' and 'M' respectively. The task is to add these two numbers and provide the sum in array format.

Input:

The first line of input indicates the integer T, which denotes the number of test cases.
The first line of each test case contains two space-separated integers, 'N' and 'M', representing the sizes of the two arrays.
The second line contains 'N' space-separated integers indicating the elements of the first array.
The third line contains 'M' space-separated integers indicating the elements of the second array.

Output:

A single line for each test case that contains space-separated digits describing the sum of the two numbers A and B.

Example:

Input:
1
3 3
4 5 1
3 4 5
Output:
7 9 6

Explanation: The integer represented by A[] = {4, 5, 1} is 451 and B[] = {3, 4, 5} is 345. Their sum is 796, which should be returned as {7, 9, 6}.

Constraints:

  • 1 <= T <= 10^2
  • 1 <= N, M <= 10^4
  • 0 <= A[i], B[i] <= 9
  • Time Limit: 1 sec

Note:

1. Each array's first index represents the most significant digit of the number.
2. Numbers do not contain leading zeros, and the resultant sum should also be free of leading zeros.
AnswerBot
2d

Given two numbers represented as arrays, calculate their sum and return the result as an array.

  • Iterate through the arrays from right to left, adding digits and carrying over if necessary

  • Handle cases w...read more

Help your peers!
Add answer anonymously...
InterviewBit Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter