Ctrl K

Add a Number to a Digit Array

Easy
A non-negative integer is stored in the array digits, one digit per element, from the most significant to the least significant. For instance, 4096 is stored as [4, 0, 9, 6]. You are also given an integer amount. Return the sum of the two numbers in the same format: an array of digits, most significant first.
Example 1
digits=[3, 0, 7], amount=95 → [4, 0, 2]

307 + 95 = 402.

Example 2
digits=[9, 9], amount=1 → [1, 0, 0]

99 + 1 = 100: the answer has one more digit.

Example 3
digits=[0], amount=4200 → [4, 2, 0, 0]

0 + 4200 = 4200, longer than the original array.

Constraints
  • 1 ≤ digits.length ≤ 2·10⁵
  • 0 ≤ digits[i] ≤ 9
  • digits has no leading zeros, except for the number 0 itself
  • 1 ≤ amount ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsAdd One to a Digit Array EasyThe Lost Ticket EasyCorner Stamps on a Table EasyBest Product of Three EasyThe Biggest Triangle on the Board Easy
loading Python…
Input
digits=[3, 0, 7], amount=95
Expected
[4, 0, 2]

Run checks the first 3 tests in your browser. Submit checks all 50 tests on the server, hidden ones included.