Add to Array-Form of Integer
Real-world version: Advancing A Digit Strip CounterThe 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.
def add_to_array_form(digits: list[int], step: int) -> list[int]:public List<Integer> addToArrayForm(int[] digits, int step)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 11 test cases: 11 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's