A utility stores each meter reading as a singly linked chain of decimal digits, most-significant digit at the head (natural reading order). Given two such readings, compute their sum and output it as a digit chain, most-significant digit first, with no leading zeros (the value zero is the single digit 0). Input chains may contain leading zeros; the value is the number they spell.
Line 1: an integer p, the number of digits in reading A (p >= 1).
Line 2: p space-separated digits (each 0-9) of reading A, most-significant first.
Line 3: an integer q, the number of digits in reading B (q >= 1).
Line 4: q space-separated digits (each 0-9) of reading B, most-significant first.
Line 1: the number of digits in the sum. Line 2: the digits of the sum, most-significant first, space-separated, with no leading zeros.
Example 1
Input
3 1 2 3 2 4 5
Expected
3 1 6 8
Explanation
123 + 45 = 168, output most-significant first as 1 6 8.
Example 2
Input
2 9 9 1 1
Expected
3 1 0 0
Explanation
99 + 1 = 100, which carries into a new leading digit: 1 0 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →