16. Plus One

EasyArrayArray

You are given a large integer as an array digits, where each digits[i] is the i-th digit (most to least significant, no leading zeros). Increment the integer by one and return the resulting digit array.

Input: An integer array digits of length n where each element is 0–9.

Output: Integer array representing the incremented number.

Examples

Example 1
Input: [1,2,3]
Output: [1,2,4]
Explanation: Step 1: Last digit 3+1=4, no carry. Result: [1,2,4].
Example 2
Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: Step 1: Last digit 1+1=2, no carry. Result: [4,3,2,2].
Example 3
Input: [9,9,9]
Output: [1,0,0,0]
Explanation: Step 1: 9+1=10, write 0 carry 1.
Step 2: 9+1=10, write 0 carry 1.
Step 3: 9+1=10, write 0 carry 1.
Step 4: Carry left → prepend 1. Result: [1,0,0,0].

Constraints

Asked by

BloombergGoogleMetaCapgeminiAmazonAccenture
Solve this problem in the editor →