Back to posts

LeetCode Challenge Day 108 β€” 66. Plus One

Nitin Ahirwal / January 1, 2026

LeetCode ChallengeDay 108ArraysMathJavaScriptEasy

Hey folks πŸ‘‹

This is Day 108 of my LeetCode streak πŸš€
Today's problem is 66. Plus One β€” an easy problem on paper, but a great test of clean thinking and edge-case handling.


πŸ“Œ Problem Statement

You are given:

  • An array digits representing a non-negative integer
  • Each element is a single digit (0–9)
  • Digits are ordered from most significant to least significant
  • No leading zeroes

Goal:
Increment the number by one and return the resulting digits array.


πŸ’‘ Intuition

This problem is just elementary addition, applied to an array.

Key observations:

  • Addition starts from the last digit
  • If a digit is less than 9, increment it and stop
  • If a digit is 9, it becomes 0 and generates a carry
  • If all digits are 9, the carry propagates through the entire array and creates a new leading 1

So the entire problem boils down to handling carry correctly.


πŸ”‘ Approach

  1. Traverse the array from right to left
  2. For each digit:
    • If it’s less than 9, increment it and return immediately
    • Otherwise, set it to 0 and continue
  3. If the loop completes, all digits were 9
    • Insert 1 at the beginning of the array
  4. Return the updated array

This avoids converting the array into a number and works efficiently for large inputs.


⏱️ Complexity Analysis

  • Time Complexity:
    O(n) β€” single pass through the digits

  • Space Complexity:
    O(1) β€” in-place modification (excluding output)


πŸ§‘β€πŸ’» Code (JavaScript)

/**
 * @param {number[]} digits
 * @return {number[]}
 */
var plusOne = function(digits) {
    for (let i = digits.length - 1; i >= 0; i--) {
        if (digits[i] < 9) {
            digits[i]++;
            return digits;
        }
        digits[i] = 0;
    }
    
    // If all digits were 9
    digits.unshift(1);
    return digits;
};

🎯 Reflection

This problem reinforces an important lesson:

  • Simple problems test discipline, not difficulty

  • Carry handling is a recurring pattern in many harder problems

  • Clean early returns keep the code readable

That wraps up Day 108 of my LeetCode challenge πŸ”₯
On to Day 109 β€” consistency beats intensity πŸš€

Happy Coding πŸ‘¨β€πŸ’»