Skills101
← All coding problems

Add One to a Digit Array

easyArrays & Prefix Sums+10 XPTime limit 2s

A very large number is stored as an array of its digits, most significant digit first (so [1, 2, 9] is 129). Add 1 to the number and print the digits of the result.

Input:

  • line 1: n — number of digits
  • line 2: the n digits, separated by spaces (no leading zeros, except the number 0 itself)

Output: the digits of the number plus one, separated by spaces.

Constraints: 1 ≤ n ≤ 10⁵, each digit is 0–9. The number is far too large for any integer type.

Think about it: add from the right. A 9 becomes 0 and the carry moves left; the first digit that isn't 9 just goes up by one and you stop. If every digit was 9, the answer is a 1 followed by n zeros.

Sample input 1

3
1 2 9

Sample output 1

1 3 0

Sample input 2

3
9 9 9

Sample output 2

1 0 0 0

Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).

Loading editor…

Log in to run and submit your code — it's free.