Plus One
Add one to a number stored as an array of digits, handling the carry from right to left.
01The problem
A big number is stored as an array digits, most significant digit first ([1, 2, 3] means 123). Add one and return the new digits.
| Input | Output |
|---|---|
[1, 2, 3] | [1, 2, 4] |
[1, 9, 9] | [2, 0, 0] |
[9, 9] | [1, 0, 0] |
The number can have up to 100 digits, so it won't fit in a long.
02How to spot it
You're doing school addition by hand: start at the right, and a 9 becomes 0 with a carry. Walk the array backwards.
03Pseudocode
for i from last index down to 0:
if digits[i] < 9:
digits[i] += 1, return digits
digits[i] = 0 (carry to the left)
all were 9: return [1, 0, 0, …]- A digit below 9 just goes up by one, and we're done: nothing carries.
- A 9 becomes 0 and the carry moves one place left.
- If every digit was 9, the answer is a 1 followed by zeros.
04Watch it work
Adding one starts at the last digit, exactly like adding on paper.
Pseudocode
- for i from last index down to 0:
- if digits[i] < 9:
- digits[i] += 1, return digits
- digits[i] = 0 (carry to the left)
- all were 9: return [1, 0, 0, …]
Variables
- i
- 2
05Step-by-step dry run
- 1109192i
Adding one starts at the last digit, exactly like adding on paper.
i = 2
- 2109102i
digits[2] is 9. 9 + 1 = 10, so write 0 here and carry 1 to the left.
i = 2digits[i] = 0
- 3100102i
digits[1] is 9. 9 + 1 = 10, so write 0 here and carry 1 to the left.
i = 1digits[i] = 0
- 4200102i
digits[0] is below 9, so it takes the +1: it becomes 2. No carry left, so we're done.
i = 0digits[i] = 2
06Java solution
import java.util.Arrays;
public class PlusOne {
static int[] plusOne(int[] digits) {
for (int i = digits.length - 1; i >= 0; i--) {
if (digits[i] < 9) {
digits[i]++;
return digits;
}
digits[i] = 0;
}
int[] bigger = new int[digits.length + 1]; // all zeros
bigger[0] = 1;
return bigger;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(plusOne(new int[] {1, 2, 3})));
System.out.println(Arrays.toString(plusOne(new int[] {1, 9, 9})));
System.out.println(Arrays.toString(plusOne(new int[] {9, 9})));
}
}[1, 2, 4] [2, 0, 0] [1, 0, 0]
The tricky line: new int[digits.length + 1] is already all zeros in Java, so setting bigger[0] = 1 is all we need for 99…9.
07Why it works
Adding one only changes the trailing 9s (they become 0) and the first non-9 digit from the right (it goes up by one). Everything left of that stays the same, so we can stop early.
08Time and space
- Time: O(n) in the worst case (all 9s), but usually O(1): most numbers don't end in 9.
- Space: O(1), except the new array when every digit is 9.
09Edge cases
| Case | Result |
|---|---|
[0] | [1] |
[9] | [1, 0] |
| No 9s at end | Only last digit changes |
10Common mistakes
- Converting to
intorlong. 100 digits overflow any primitive. - Forgetting the all-9s case. The array has to grow by one.
11Other approaches
| Approach | Time | Note |
|---|---|---|
BigInteger | O(n) | Works, but interviewers want the carry logic |
| Backward carry, stop early | O(n) | Simple and expected |
12In the interview
13Check your understanding
Quiz · 2 questions
0 of 2 answered
1. What does plusOne return for [4, 9]?
2. When does the answer need a longer array?
14Solve it yourself
Try it on LeetCode #66.