Show problems as
Classic problem #989EasyArrayMath

Add to Array-Form of Integer

Real-world version: Advancing A Digit Strip Counter
Tracked in this browser only
Read Add to Array-Form of Integer on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A mechanical counter shows its reading as digits, one decimal digit per wheel, listed from the most significant wheel to the least significant one. The reading carries no leading zero unless it is the single digit 0.

The counter is now advanced by step units. Return the new reading in the same form, most significant digit first and with no leading zero. The strip gains a wheel at the front when the reading needs one.

Examples

Example 1

Input
digits = [9, 9, 9], step = 1
Output
[1, 0, 0, 0]

The reading 999 advanced by 1 unit becomes 1000, which needs a fourth wheel, so the strip is [1, 0, 0, 0].

Example 2

Input
digits = [1, 4, 0, 7], step = 93
Output
[1, 5, 0, 0]

The reading 1407 advanced by 93 units becomes 1500, so the strip is [1, 5, 0, 0].

Example 3

Input
digits = [0], step = 10000
Output
[1, 0, 0, 0, 0]

The reading 0 advanced by 10000 units becomes 10000, so four wheels are added at the front.

Constraints

  • 1 <= digits.length <= 10^4
  • 0 <= digits[i] <= 9
  • digits has no leading zero unless it is the single digit 0.
  • 1 <= step <= 10^4

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def add_to_array_form(digits: list[int], step: int) -> list[int]:
Java
public List<Integer> addToArrayForm(int[] digits, int step)