Plus One looks trivial — add 1 to a number — but the number is stored as an array of digits, so you have to handle carrying by hand. It is a clean lesson in simulating grade-school addition.
Problem. You are given a large integer as an array of its digits digits, most significant digit
first. Increment the integer by one and return the resulting array of digits.
Example: digits = [1, 2, 9] → answer [1, 3, 0] (because 129 + 1 = 130).
The slow way first
The tempting shortcut: join the digits into a string, convert to an integer, add 1, then split it back into digits. That works for small inputs, but the whole reason the number is given as an array is that it can be bigger than any built-in integer type can hold. Converting defeats the purpose, and in languages without arbitrary-precision ints it overflows.
The question to ask: how do I add 1 by hand? I start at the rightmost digit and carry leftward only as far as I need to.
The idea: carry from the right
Walk the digits right to left. At each digit:
- If it is
9, adding 1 turns it into10, so write0and carry to the next digit left. - If it is anything else, just add 1 — there is no more carry, so return immediately.
If you fall off the left end still carrying (the number was all 9s, like 999), prepend a 1.
The key insight: a carry only travels through a run of trailing 9s. The moment we hit a non-9 digit, we are done — most of the time after touching just one digit.
Walk through it
Step through the animation. The pointer i starts at the rightmost digit and moves left. The trailing 9 becomes 0 and carries; the 2 becomes 3 with no carry, so we stop and return [1, 3, 0].
Pseudocode
i = last index of digits
while i is a valid index:
if digits[i] is 9:
set digits[i] to 0 # carry to the left
move i one step left
else:
add 1 to digits[i] # no carry needed
return digits
return [1] + digits # all digits were 9, prepend a new 1The Python solution
def plus_one(digits):
i = len(digits) - 1
while i >= 0:
if digits[i] == 9:
digits[i] = 0
i -= 1
else:
digits[i] += 1
return digits
return [1] + digitsistarts at the last index so we add from the least significant digit.- The
whileloop only keeps running while we are still carrying through9s. - A
9becomes0and we step left — the carry rolls on. - The first non-9 digit gets
+= 1and we return right away; no need to touch the rest. - If the loop runs off the left end, every digit was a
9, so we prepend a fresh1(e.g.[9, 9]→[1, 0, 0]).
Complexity
| Case | Time | Notes |
|---|---|---|
| Best (no trailing 9) | O(1) (fast) | increment one digit and return |
| Worst (all 9s) | O(n) (moderate) | carry through every digit |
O(1) (fast)We modify the array in place, so no extra space beyond the output — and only in the all-9s case (like 999) do we allocate one new slot for the prepended 1.
When this pattern shows up
Any time you simulate arithmetic on digits or do addition with a carry — adding two numbers stored as arrays or linked lists, incrementing a binary counter — the move is the same: process from the least significant end and propagate the carry only as far as it reaches.
Do not forget the all-9s case. If you only loop and never handle a carry that survives past index 0, an
input like [9, 9, 9] returns [0, 0, 0] instead of [1, 0, 0, 0].
Practice
For digits = [1, 2, 9], after the rightmost 9 becomes 0, what happens at the next digit to the left?
1. Why do we process the digits from right to left?
2. When we hit a digit that is not 9, what do we do?
3. What does the input [9, 9] return?
4. What is the worst-case time complexity?