Bump a Digit-Array Number Up by One
Arrays & HashingAuto-graded · 22 test casesLast updated
Increment a large non-negative number stored as a digit list by one, handling carry propagation and length growth without converting to an integer.
Increment a Number Stored as Digits
A non-negative integer is stored in a list of decimal digits, with the most significant digit first (at index 0). For example, the number 407 is stored as [4, 0, 7].
Write a function that adds exactly one to this number and returns the digits of the result in the same format.
Rules
- Do the arithmetic digit by digit, carrying as needed. Do not convert the whole list into a single integer and do not build the result with a growing, dynamically resized collection. The result should be sized correctly up front.
- If the addition makes the number longer (for example
[9, 9]becomes[1, 0, 0]), return a list that is one element longer than the input. - Otherwise the returned list has the same length as the input.
- The returned list must not have leading zeros, except when the value itself is zero. Given the constraints, the only zero input is
[0], and its answer is[1]. - The input list should be left unchanged; return a new list.
Constraints
1 <= len(digits) <= 10000 <= digits[i] <= 9- The input has no leading zeros unless it is exactly
[0].
Examples
Example 1
Input: increment_digits([1, 2, 3])
Output: [1, 2, 4]
Example 2
Input: increment_digits([9, 9])
Output: [1, 0, 0]
Example 3
Input: increment_digits([8, 9, 9])
Output: [9, 0, 0]
Hints
Hint 1
Adding one only affects the trailing run of 9s and the digit immediately before that run. Everything further left stays the same.
Hint 2
Walk from the last index toward the front. If a digit is below 9, increment it and you can return immediately. If it is 9, set it to 0 and keep moving left.
Hint 3
If the loop finishes without returning, every digit was 9 and is now 0. The answer is a 1 followed by all those zeros, so the length grows by one.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is grade-school addition of 1 with a carry. Make a copy of the input, then scan from the least significant digit (the end) toward the front:
- If the current digit is less than 9, increment it and return. The carry is absorbed, so all digits to the left are unchanged.
- If the current digit is 9, it becomes 0 and the carry moves one position left.
- If the scan passes index 0 with the carry still pending, every digit was 9 and is now 0. The answer is
1followed by those zeros, a list of lengthn + 1.
Why it is correct
Write the number as prefix, d, 9...9, where d is the rightmost digit that is not 9. Adding one turns the trailing nines into zeros and raises d by one. Since d <= 8, the new digit is at most 9, so no further carry occurs and the prefix is untouched. The loop does exactly this.
If no such d exists, the number is 10^n - 1, and adding one gives 10^n: a 1 followed by n zeros. The loop has already zeroed every position, so prepending 1 gives the right result.
No leading zeros appear: the first digit is only changed to 0 in the all-nines case, where a leading 1 is added. [0] becomes [1].
Complexity
- Time: O(n). The loop usually stops after the trailing nines plus one step, but copying the list costs O(n).
- Space: O(n) for the returned list; beyond that only a loop index is used.
Reference solution
from typing import List
def increment_digits(digits: List[int]) -> List[int]:
result = digits[:]
for i in range(len(result) - 1, -1, -1):
if result[i] < 9:
result[i] += 1
return result
result[i] = 0
return [1] + result