Add to Array-Form of Integer (LeetCode 989): Solution and Explanation
num is a number written as an array of digits, and k is an ordinary integer. Return num + k as an array of digits.
Intuition
num can have 10,000 digits, far too many for a 64-bit integer, so do what you would on paper: add column by column from the right, keeping a carry.
The neat trick is to let k be the carry. Add the current digit of num into k. The ones digit of k is the digit to write in this column, and k // 10 is everything that moves one column left: the real carry plus the digits of the original k that have not been used yet.
Keep going while num still has digits or k is still positive. That covers both a short num with a big k and a final carry such as 999 + 1. Digits come out ones first, so reverse them at the end.
Approach: Column addition with k as the carry
- Set
ito the last index of num and start an empty result list. - While
i >= 0ork > 0: ifi >= 0, addnum[i]intok. - Append
k % 10to the result, then setk = k // 10and moveione step left. - Reverse the result, since it was built from the ones digit upward.
Step-by-step example
num = [1,2,0,0], k = 34
- i = 3: k = 34 + 0 = 34. Write 4, k becomes 3.
- i = 2: k = 3 + 0 = 3. Write 3, k becomes 0.
- i = 1: k = 0 + 2 = 2. Write 2, k becomes 0.
- i = 0: k = 0 + 1 = 1. Write 1, k becomes 0.
- i = -1 and k = 0, so stop. result = [4,3,2,1]; reversed it is [1,2,3,4].
Code: Python, Java, C++
1class Solution:
2 def addToArrayForm(self, num: List[int], k: int) -> List[int]:
3 result = []
4 i = len(num) - 1
5 while i >= 0 or k > 0:
6 if i >= 0:
7 k += num[i]
8 result.append(k % 10)
9 k //= 10
10 i -= 1
11 return result[::-1]1class Solution {
2 public List<Integer> addToArrayForm(int[] num, int k) {
3 List<Integer> result = new ArrayList<>();
4 int i = num.length - 1;
5 while (i >= 0 || k > 0) {
6 if (i >= 0) k += num[i];
7 result.add(k % 10);
8 k /= 10;
9 i--;
10 }
11 Collections.reverse(result);
12 return result;
13 }
14}1class Solution {
2public:
3 vector<int> addToArrayForm(vector<int>& num, int k) {
4 vector<int> result;
5 int i = num.size() - 1;
6 while (i >= 0 || k > 0) {
7 if (i >= 0) k += num[i];
8 result.push_back(k % 10);
9 k /= 10;
10 i--;
11 }
12 reverse(result.begin(), result.end());
13 return result;
14 }
15};Time and space complexity
O(max(n, log k))
Each loop iteration writes one output digit, and the output has max(n, digits of k) + 1 digits at most.
O(max(n, log k))
Only the output list grows; the carry is a single integer.
Other approaches
Separate carry variable (time O(max(n, log k)), space O(max(n, log k)))
Peel k's digits with k % 10 alongside num's digits and keep a separate carry of 0 or 1, exactly like Add Strings. Same complexity, a little more bookkeeping.
Big integer conversion (time O(n) to O(n^2) depending on the library, space O(n))
Join the digits into a big integer, add k and split the result back into digits. Works in Python or with BigInteger, but it hides the algorithm the problem is testing and is slower for long inputs.
Common mistakes
- Stopping when num runs out while k still has digits, which drops the leading digits for inputs like [0] + 10000.
- Forgetting the final carry, so 999 + 1 becomes [0,0,0] instead of [1,0,0,0].
- Inserting at the front of an array-backed list on every step, which makes the loop quadratic. Append and reverse once instead.
- Converting num to a 32-bit or 64-bit integer, which overflows for long arrays.
Frequently asked questions
Why can k act as the carry?
After adding num[i], k holds this column's total plus the unprocessed higher digits of the original k scaled by 10. Its ones digit belongs in this column, and k // 10 is exactly what the next column needs to add.
Can k overflow?
No. k starts at most 10,000 and each step adds at most 9 before dividing by 10, so it stays small.
Why reverse at the end instead of inserting at the front?
Appending is O(1) amortized, while inserting at the front of an array list shifts every element. One reverse at the end keeps the whole algorithm linear.