LeetCode Challenge Day 108 β 66. Plus One
Nitin Ahirwal / January 1, 2026
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
digitsrepresenting 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 becomes0and generates a carry - If all digits are
9, the carry propagates through the entire array and creates a new leading1
So the entire problem boils down to handling carry correctly.
π Approach
- Traverse the array from right to left
- For each digit:
- If itβs less than
9, increment it and return immediately - Otherwise, set it to
0and continue
- If itβs less than
- If the loop completes, all digits were
9- Insert
1at the beginning of the array
- Insert
- 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 π¨βπ»