LeetEye LeetEye
Easy Math & Geometry ~5 min

Plus One

math-and-geometry plus-one

Problem

You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.

Increment the large integer by one and return the resulting array of digits.

Examples

Example 1
Input: digits = [1,2,3]
Output: [1,2,4]
123 + 1 = 124
Example 2
Input: digits = [4,3,2,1]
Output: [4,3,2,2]
Example 3
Input: digits = [9]
Output: [1,0]
Key Insight

Simulate addition from right to left. Handle carry propagation.

From right: if digit < 9, increment and return. If 9, set to 0 and continue. If all 9s, prepend 1.

How to Approach This Problem

Pattern Recognition: When you see keywords like plus one math-and-geometry, think Math & Geometry.

Step-by-Step Reasoning

1 We add 1 starting from:
Answer: Right (least significant digit)
Addition starts from rightmost digit (ones place).
2 Carry occurs when:
Answer: B and C (same thing)
9 + 1 = 10. Set digit to 0, carry 1 to next position.
3 If digit < 9:
Answer: Increment and return immediately
No carry will happen. Just increment and we're done.
4 For [9, 9, 9]:
Answer: Result is [1, 0, 0, 0]
999 + 1 = 1000. Array grows by one element.
5 Array needs to grow when:
Answer: All digits are 9
Only when carry propagates all the way through.

Solution

def plusOne(digits: List[int]) -> List[int]:
    for i in range(len(digits) - 1, -1, -1):
        if digits[i] < 9:
            digits[i] += 1
            return digits
        digits[i] = 0
    
    # All 9s case: [9,9,9] -> [1,0,0,0]
    return [1] + digits

Complexity Analysis

Time O(n)
Space O(n)

Master This Pattern

Build intuition with interactive MCQs. Practice Math & Geometry problems in the LeetEye app.

Download LeetEye Free
Practice in LeetEye