The array-form of an integer num is an array representing its digits in left to right order.
num = 1321, the array form is [1,3,2,1].Given num, the array-form of an integer, and an integer k, return the array-form of the integer num + k.
Example 1:
Input: num = [1,2,0,0], k = 34 Output: [1,2,3,4] Explanation: 1200 + 34 = 1234
Example 2:
Input: num = [2,7,4], k = 181 Output: [4,5,5] Explanation: 274 + 181 = 455
Example 3:
Input: num = [2,1,5], k = 806 Output: [1,0,2,1] Explanation: 215 + 806 = 1021
Constraints:
1 <= num.length <= 1040 <= num[i] <= 9num does not contain any leading zeros except for the zero itself.1 <= k <= 104When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We're given a number as a list of digits and another number. The brute force approach is to first convert the digit list into a single big number. Then, add the two numbers together and convert the result back into a list of digits.
Here's how the algorithm would work step-by-step:
def add_to_array_form_of_integer(digit_list, number_to_add):
# Convert the digit list into a single large number
large_number = 0
for digit in digit_list:
large_number = large_number * 10 + digit
# Add the other given number to this large number
sum_of_numbers = large_number + number_to_add
# Convert the sum back into a list of digits
result_list = []
if sum_of_numbers == 0:
result_list.append(0)
else:
while sum_of_numbers > 0:
# Extract the last digit
digit = sum_of_numbers % 10
result_list.append(digit)
# Remove the last digit
sum_of_numbers //= 10
# Reverse the list to get the correct order
result_list.reverse()
return result_listImagine adding the number K to the array as if you're doing it by hand, column by column. We start from the end and work our way to the beginning, handling any carry-over values as we go. This lets us directly construct the result without needing to convert the array into a single large number.
Here's how the algorithm would work step-by-step:
def addToArrayForm(number_array, k_value):
result_array = []
array_length = len(number_array) - 1
carry_over = 0
while array_length >= 0 or k_value > 0:
current_sum = carry_over
if array_length >= 0:
current_sum += number_array[array_length]
array_length -= 1
if k_value > 0:
current_sum += k_value % 10
k_value //= 10
# If sum >= 10, we extract the digit and propagate carry.
result_array.append(current_sum % 10)
carry_over = current_sum // 10
# Add any final carry to the result.
if carry_over > 0:
result_array.append(carry_over)
result_array.reverse()
return result_array| Case | How to Handle |
|---|---|
| Empty input array num | Treat an empty input array as the integer 0 and proceed with the addition with k. |
| k is zero | Return the original array num if k is zero, as no addition is needed. |
| Large value of k that results in integer overflow in intermediate calculations | Perform digit-by-digit addition and carry-over to avoid exceeding integer limits. |
| The input array num contains leading zeros | Remove the leading zeros from the array at the beginning or during the processing. |
| The sum has more digits than either k or the number represented by num | Ensure the algorithm correctly handles carry-overs that propagate beyond the most significant digit and extend the array if necessary. |
| Input array num contains only a single '0' | Adding to this should not result in leading zeros. |
| Large array size for num (performance consideration) | Use an efficient algorithm that avoids unnecessary memory allocation or copying to maintain acceptable performance for large arrays. |
| k is a single digit number and the last element in array num + k is more than 9 | Ensure carry is handled correctly when only the last digit changes when k is added. |