In this problem, We have given a non-empty array which represents a non-negative integer. Write a code to add one to the number.
Each element in the array contain a single digit. You may assume the integer does not contain any leading zero, except the number 0 itself.
Example 1 –
Input: [1, 3, 4]
Output: [1, 3, 5]
In this example, the array represents the number 134. When we add 1 to it, the new number is 135.
Example 2 –
Input : [1, 2, 9]
Output: [1, 3, 0]
The number is 129. When we add 1 to 129. The new number is 130.
Example 3 –
Input : [9, 9, 9]
Output: [1, 0, 0, 0]
For the number 999, when you add 1, the new number would be 1000. If you think in terms of array, the new array size would be the previous array size plus one.