Skip to main content

Plus One - Challenge

Plus One

Easy
Problem
You are given a large integer represented as an array of digits digits, ordered most significant to least significant. The number has no leading 0 unless it is exactly zero. Increment the integer by one and return the resulting array of digits.
Constraints
  • 1 <= digits.length <= 100
  • 0 <= digits[i] <= 9
  • digits has no leading 0 unless digits.length == 1 and digits[0] == 0
Sample Tests
Test 1
Input
digits = [1, 2, 3]
Expected
[1, 2, 4]

123 + 1 = 124

Test 2
Input
digits = [9]
Expected
[1, 0]

Carry creates a new most significant digit.

Test 3
Input
digits = [9, 9, 9]
Expected
[1, 0, 0, 0]

Cascading carry across all digits.

Console

Run tests to see verdicts.