Skip to content
AI360Xpert
Beta
Difficulty: EasyMath & Geometry

Plus One

Problem in Plain English

Add one to a non-negative integer stored as an array of its decimal digits, most significant first, and return the resulting digit array.

Problem Statement

You are given a large non-negative integer as an array digits, where each entry holds one decimal digit ordered from most significant to least significant. Add one to the integer and return the resulting array of digits.

The integer can be too long for any native number type, so the addition must happen digit by digit. A trailing run of 9 digits rolls over to 0 and carries left, and an input of all 9 digits grows the array by one ([9] becomes [1, 0]).

Constraints

  • 1≤digits.length≤1001 \le \text{digits.length} \le 100
  • 0≤digits[i]≤90 \le \text{digits[i]} \le 9
  • The representation has no leading zero except the single-digit value [0].

Examples

Example 1

Input
digits = [1, 2, 3]
Output
[1, 2, 4]

The array represents 123. Adding one gives 124, so only the final digit changes and the result is [1, 2, 4].

Example 2

Input
digits = [4, 3, 2, 1]
Output
[4, 3, 2, 2]

The array represents 4321. Adding one gives 4322; the final 1 absorbs the increment without carrying left.

Example 3

Input
digits = [9]
Output
[1, 0]

Adding one to 9 produces 10. The original digit rolls over to 0, and the carry becomes a new leading 1.

Example 4

Input
digits = [1, 2, 9, 9]
Output
[1, 3, 0, 0]

The units and tens digits each roll from 9 to 0. Their carry reaches the hundreds digit, increasing 2 to 3; the leading 1 stays unchanged.

Intuition

Start at the units digit, where adding one first acts. A digit below 9 absorbs the increment and leaves everything to its left unchanged. A 9 becomes 0 and sends one carry left; for [1, 2, 9, 9] that changes the last two digits to zeros and the 2 to 3. Example 1 has no trailing 9, so only its 3 changes to 4. If every digit rolls over, a new leading 1 is required.

Approaches

Convert and Add

Solution Details

Reveal Convert and Add: intuition, complexity, and code

Hints

Hint 1
Add one on paper to 129: only the trailing 9 flips, then the 2 absorbs the carry.
Hint 2
Scan from the last digit leftward: turn each 9 into 0 and stop at the first digit below 9 by adding one to it.
Hint 3
If every digit was 9, nothing absorbed the carry, so the answer is 1 followed by all 0 digits in a new array.

Edge Cases

  • [0] increments to [1] and retains the original object in the carry approach.
  • A single 9 becomes [1, 0]; an all-9 array of length 100 becomes a leading 1 followed by 100 zeros.
  • A suffix of nines, as in [1, 2, 9, 9], rolls over only until the first earlier non-9 digit absorbs the carry.
  • The conversion approach always allocates a fresh result and leaves the input unchanged. The carry approach returns the mutated input except for all nines, when it returns a new array and leaves the original all zeros.

Common Mistakes and Interview Tips

  • Folding the digits into a 32-bit int or 64-bit long and adding one, which overflows once the input passes 10 or 19 digits.
  • Returning the original array unchanged after an all-9 input instead of allocating the longer [1, 0, ..., 0] result.
  • Continuing the scan after a digit below 9 absorbs the increment, flipping earlier digits that should stay fixed.
  • Using JavaScript Number for conversion silently rounds long integers; BigInt is required for the conversion approach.
  • Interview tip: state whether mutation is allowed. A caller needing the original array must copy it before using the carry approach, adding O(n) storage.

Key Takeaway

Digit-array arithmetic runs right to left so carries flow naturally, and the all-max-digit input always needs its own branch.