Back to problems
#66Easy·math·3 min read

Plus One

arraymath
Share

This problem is a great way to practice how arrays and manual arithmetic work in programming.

The goal is to take an array of digits representing a large integer (like [1, 2, 3]), add one to it, and return the result ([1, 2, 4]). The "trick" is handling cases like [1, 9, 9], where adding one causes a carry to ripple through the number.


Step 1: The Logic of Addition

When we add numbers on paper, we start from the right (the last index) and move to the left.

  1. Add 1 to the last digit.
  2. If the digit becomes 10, it turns into 0, and we carry the 1 to the next digit on the left.
  3. If the digit is less than 10, we are done! We can just return the array immediately.

Step 2: Handling the "Carry" in Code

We use a for loop that starts at the end of the array and moves backward.

for (let i = digits.length - 1; i >= 0; i--) {
    // 1. Add 1 to the current digit
    digits[i]++;
 
    // 2. Check if there is a carry
    if (digits[i] < 10) {
        // No carry needed! We can stop and return the array.
        return digits;
    } else {
        // It was a 9, so now it's a 10. 
        // We set it to 0 and the loop continues to the next digit (the carry).
        digits[i] = 0;
    }
}

Step 3: The "Edge Case" (All Nines)

What happens if the input is [9, 9, 9]?

  1. The last 9 becomes 0.
  2. The middle 9 becomes 0.
  3. The first 9 becomes 0.
  4. The loop ends, but we still have a carry of 1 that needs a place to go!

To solve this, if the loop finishes without returning, it means every digit turned into a 0. We need to add a 1 at the very beginning of the array. In JavaScript, we can use .unshift() or just create a new array.


Step 4: The Final JavaScript Solution

/**
 * @param {number[]} digits
 * @return {number[]}
 */
var plusOne = function(digits) {
    // Iterate from right to left
    for (let i = digits.length - 1; i >= 0; i--) {
        
        // If the digit is less than 9, just add 1 and we are done!
        if (digits[i] < 9) {
            digits[i]++;
            return digits;
        }
        
        // If the digit is 9, it becomes 0 (carrying the 1 to the next loop iteration)
        digits[i] = 0;
    }
 
    // If we reach this point, it means the number was something like [9, 9, 9]
    // which has now become [0, 0, 0]. We need to add a 1 at the front.
    digits.unshift(1);
    return digits;
};

Why not just convert the array to a Number?

You might be tempted to do this: let num = Number(digits.join('')) + 1;

Warning: This will fail for very large arrays. JavaScript's Number type (and even BigInt to an extent in some environments) has precision limits. If the array has 100 digits, Number() will lose accuracy and give you the wrong answer. Working directly with the array is the safest and most efficient "interview-style" way to solve it.

Summary of the "Steps" in your head:

  1. Loop backwards through the array.
  2. Is it a 9? Turn it to 0 and keep going.
  3. Is it not a 9? Increment it by 1 and stop/return immediately.
  4. Finished the loop? That means you had all 9s. Put a 1 at the front of the array.

You might also like