Browse curriculum

Plus One

Add one to a number stored as an array of digits: walk from the end, turn trailing 9s into 0s, and grow the array only when every digit was 9.

Problem Understanding

Plus One: a non-negative integer is given as an array digits, most significant digit first, with no leading zeros. Return the array for that integer plus one.

Examples: [1,2,3] returns [1,2,4], [4,3,2,1] returns [4,3,2,2], and [9] returns [1,0].

The Intuition: The Carry Stops at the First Digit Below 9

Adding 1 by hand only changes digits from the right until a digit can absorb it:

  1. A digit below 9 takes the +1 and nothing else changes, so you can return immediately.
  2. A 9 becomes 0 and the 1 carries one place left.
  3. If the carry runs off the front, every digit was 9; the answer is a 1 followed by that many zeros.

It is Add Strings with the second number fixed at 1, so the loop can stop early instead of always walking the whole array. The same right-to-left carry drives Add Binary and Multiply Strings.

Interactive Walkthrough

i marks where the carry is. 9s that rolled over to 0 are tinted, and the digit that took the +1 turns green. Try All nines to see the array grow.

Dry Run Table

Input: digits = [8, 9, 9, 9]

i digits[i] Action digits after
3 9 set 0, carry [8, 9, 9, 0]
2 9 set 0, carry [8, 9, 0, 0]
1 9 set 0, carry [8, 0, 0, 0]
0 8 8 + 1 = 9, return [9, 0, 0, 0]

Answer: [9, 0, 0, 0].

The Solution Template

One loop from the right that returns as soon as a digit absorbs the carry. C always returns a newly allocated array, because LeetCode frees the result.

Plus One Code
1var plusOne = function(digits) {
2 for (let i = digits.length - 1; i >= 0; i--) {
3 if (digits[i] < 9) {
4 digits[i]++;
5 return digits;
6 }
7 digits[i] = 0;
8 }
9 return [1, ...digits];
10};

Edge Cases & Common Mistakes

  • All 9s: [9, 9] becomes [1, 0, 0]; remember to grow the array.
  • Single digit: [0] becomes [1], and [9] becomes [1, 0].
  • Continuing after the +1: once a digit below 9 is incremented the answer is final; keep looping and you add 1 again.
  • Converting to an integer: up to 100 digits overflow 64-bit types.
  • Building the grown array: only the leading 1 is set; every other digit is already 0.

Summary

Walk from the last digit. A digit below 9 takes the +1 and you return; a 9 becomes 0 and the carry moves left. If every digit was 9, return a 1 followed by zeros. O(n) time, O(1) extra space except when the array grows.

More practice: DSA interview questions with answers · patterns cheat sheet

The approach, step by step

  1. Start at the last digit

    Loop i from n - 1 down to 0.

  2. Add or roll over

    If digits[i] < 9, increment it and return the array. Otherwise set digits[i] = 0 and continue left.

  3. Grow if needed

    If the loop finishes, every digit was 9: return [1] followed by the zeros.

Frequently asked questions

How do you add one to an array of digits?

Start at the last digit. If it is less than 9, add 1 and return. If it is 9, set it to 0 and move one place left, because the 1 carries. If you run past the front, every digit was 9: return a new array with a 1 followed by zeros.


Why not convert the digits to a number?

The array can have up to 100 digits, which overflows every built-in integer type. The digit-by-digit carry needs no conversion and runs in O(n).


When does the array get longer?

Only when every digit is 9, as in [9,9,9] → [1,0,0,0]. Any other input stops at the first digit below 9 from the right, so the length stays the same.

Applying for software engineering roles? Free, no sign-up:

AI resume checker — match your resume to the job description