v9.1.1
Menu

Add Two Numbers

mediumFREELinked ListsMeta DSA

Given two non-negative integers represented as arrays of digits in reverse order (least significant digit first), return their sum as an array in the same reverse order.

Each element contains a single digit. The numbers do not have leading zeros except the number 0 itself.

Output as space-separated digits.

Example

Input
2,4,3
5,6,4
Output
7 0 8
Reasoning
  • l1 and l2 are the digits of two whole numbers, ones digit first: position kk holds the coefficient of 10k10^k. So [2,4,3][2, 4, 3] means 2+4⋅10+3⋅102=3422 + 4 \cdot 10 + 3 \cdot 10^2 = 342, and [5,6,4][5, 6, 4] means 465465.
  • The answer is just 342+465=807342 + 465 = 807, written back in the same ones-first layout: 807=7+0⋅10+8⋅102807 = 7 + 0 \cdot 10 + 8 \cdot 10^2, giving 7 0 8.
  • Look at each power of ten: ones 2+5=72 + 5 = 7; tens 4+6=104 + 6 = 10, which is not a single digit, so the tens place keeps 00 and the extra 10⋅10=10010 \cdot 10 = 100 moves up to the hundreds; hundreds 3+4+1=83 + 4 + 1 = 8.
  • Notice the sum can have more digits than either input: 9999999+9999=100099989999999 + 9999 = 10009998 comes out as 8 9 9 9 0 0 0 1, eight digits from inputs of seven and four.
  • The inputs may also differ in length; a missing position simply contributes 00.

Constraints

  • 1 <= len(l1), len(l2) <= 100
  • 0 <= l1[i], l2[i] <= 9

More from Meta DSA

solution.py

Test results

Sign in to run your code against the test cases.