easyMath / GeometryPure DSA~12 min
Version Bump Increment
A release tool stores a build counter as an array of decimal digits, most significant first, to avoid integer-width limits. Bumping the build adds one, carrying across as many nines as needed.
Problem
Given a non-negative integer represented as an array of digits (most significant first, no leading zeros except the number 0), increment it by one and return the resulting digit array.
Input
An array digits of length n (1 ≤ n ≤ 10^4), each in [0, 9], no leading zeros.
Output
The digit array representing the incremented value.
Constraints
- 1 ≤ n ≤ 10,000
- No leading zeros in the input (except the single digit 0)
- Handle the all-nines carry-out
Examples
Example 1
Input
digits = [1, 2, 3]
Output
[1, 2, 4]
123 + 1 = 124, no carry.
Example 2
Input
digits = [9, 9]
Output
[1, 0, 0]
99 + 1 = 100, the carry grows the array by one digit.