Skip to content
Veltoryx Technologies
FreeProblem 3● EasyLeetCode #66 ↗2 min readUpdated 30 October 2026

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.

InputOutput
[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

text
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

Visualizer · step through the algorithmdigits = [1,9,9]
10
91
92
i

Adding one starts at the last digit, exactly like adding on paper.

Pseudocode

  1. for i from last index down to 0:
  2. if digits[i] < 9:
  3. digits[i] += 1, return digits
  4. digits[i] = 0 (carry to the left)
  5. all were 9: return [1, 0, 0, …]

Variables

i
2
step 1 / 4

05Step-by-step dry run

  1. 1
    10
    91
    92
    i

    Adding one starts at the last digit, exactly like adding on paper.

    i = 2

  2. 2
    10
    91
    02
    i

    digits[2] is 9. 9 + 1 = 10, so write 0 here and carry 1 to the left.

    i = 2digits[i] = 0

  3. 3
    10
    01
    02
    i

    digits[1] is 9. 9 + 1 = 10, so write 0 here and carry 1 to the left.

    i = 1digits[i] = 0

  4. 4
    20
    01
    02
    i

    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

PlusOne.java
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})));
    }
}
Expected output
[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

CaseResult
[0][1]
[9][1, 0]
No 9s at endOnly last digit changes

10Common mistakes

  • Converting to int or long. 100 digits overflow any primitive.
  • Forgetting the all-9s case. The array has to grow by one.

11Other approaches

ApproachTimeNote
BigIntegerO(n)Works, but interviewers want the carry logic
Backward carry, stop earlyO(n)Simple and expected

12In the interview

13Check your understanding

Quiz · 2 questions

0 of 2 answered

  1. 1. What does plusOne return for [4, 9]?

  2. 2. When does the answer need a longer array?

14Solve it yourself

Try it on LeetCode #66.