Plus One
To solve this coding challenge, you'll need to address the problem of incrementing a large number, which is represented using an array of its digits. The number is given in a left-to-right order, starting with the most significant digit. The key point is to handle cases where carrying over is necessary, especially when the digits are all 9's, converting to a higher significant place during the addition.
Explanation
Given an array where each element is a digit of a large integer in order, the challenge requires adding 1 to the number and returning the resulting array. It involves iterating over the array from the end (least significant digit) and handling carrying over effectively. For example:-
For
[1, 2, 3]
[1, 2, 4]
-
For
[4, 3, 2, 1]
[4, 3, 2, 2]
-
For
[9]
[1, 0]
To implement this solution, consider each element starting from the end, and if a value becomes 10, set it to 0 and move the carry to the next significant digit to the left.
- Initialization : Start from the last element of the list (the least significant digit).
- Iteration :
- Traverse the list from the end to the start.
- For each digit, check if it is less than 9:
- If it is, add 1 to it and return the list immediately as there's no carry-over needed.
- If the digit is 9, set it to 0 (as adding 1 would result in 10, causing a carry).
- Edge Case :
- If after the entire loop you still have carrying forward (i.e., you have handled the digit 9s in all positions), then you need to add a new most significant digit which is 1 at the beginning of the list.
- Initialization and Setup :
-
Define a function
plusOne
digits
- Calculate the length of the input array.
- Iteration Logic :
-
A loop runs from the last index (
length-1
0
- For each digit, check if it is less than 9. If so, increment the digit and return the modified array immediately.
- If the digit is 9, set it to 0 since adding 1 to it will cause a carry, and continue to the next left digit.
- Carry Handling :
- If after looping through all digits, all were 9's, there will be no returned value inside the loop.
-
In such a case, we need to handle the carry by adding a new digit
1
Detailed Steps in Pseudocode
# Function to increment a large integer represented as an array of digits
function plusOne(digits):
# length of the digits array
length = len(digits)
# Traverse the digits array from the last element to the first
for index from length-1 to 0 (decreasing):
# Check if the current digit is less than 9
if digits[index] < 9:
# Increment the digit by 1
digits[index] += 1
# Return the updated digits array as no further handling is required
return digits
else:
# Set the current digit to 0 as it was 9, and we have carry over
digits[index] = 0
# If loop completes without returning, all digits were 9
# Hence, insert 1 at the beginning of the array to handle overflow
return [1] + digits
# Example usage
example1 = [1, 2, 3]
result1 = plusOne(example1) # Output should be [1, 2, 4]
example2 = [4, 3, 2, 1]
result2 = plusOne(example2) # Output should be [4, 3, 2, 2]
example3 = [9]
result3 = plusOne(example3) # Output should be [1, 0]