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 <= 104Problem Overview: You’re given an integer represented as an array where each element is a digit. The goal is to add another integer k to this number and return the result in the same array-form format. Instead of converting the array to a large integer, perform digit-by-digit addition the same way you would do manual addition.
Approach 1: Divide and Conquer (O(n), O(1))
This approach treats the addition as repeated digit-level subproblems. Start from the least significant digit (the end of the array) and add the current digit with the last digit of k. Store the result digit and propagate the carry forward. Then reduce k using k //= 10 and continue until both the array digits and remaining carry are processed.
The key idea is decomposing the addition into smaller independent operations: digit addition and carry propagation. You iterate from right to left, update digits in place, and insert a new digit at the front if a carry remains. This avoids integer overflow and works efficiently even for very large numbers represented in arrays. Time complexity is O(n) where n is the number of digits, and extra space is O(1) ignoring the output.
This method relies on simple arithmetic operations and sequential array traversal, making it a natural fit for problems involving array manipulation and math-based digit operations.
Approach 2: Iterative Two-Pointer Technique (O(n), O(n))
This variation explicitly processes digits from both numbers using two logical pointers: one pointing to the end of the array and the other representing the current digit of k. At each step, extract k % 10 and combine it with the current array digit and carry. Append the resulting digit to a temporary list and update the carry.
The process continues until all digits from the array and integer k are consumed. Since digits are appended in reverse order, reverse the result at the end to produce the final array-form number. This mirrors the classic two-pointer digit addition used in problems like adding linked list numbers.
The algorithm performs a single pass across the digits, giving O(n) time complexity. The temporary result list requires O(n) extra space. This approach is often easier to reason about because each step explicitly models digit addition with carry using a forward iteration pattern common in two-pointer techniques.
Recommended for interviews: The iterative digit addition approach is what interviewers expect. It demonstrates that you understand how to simulate arithmetic operations directly on arrays without converting to large integers. Showing the straightforward carry-based solution first proves your grasp of the problem, while the optimized single-pass method highlights clean implementation and strong handling of edge cases such as leftover carry or extra digits in k.
This approach involves breaking down the problem into smaller sub-problems, solving each sub-problem recursively, and combining the results to solve the larger problem. It's often used in sorting and searching algorithms, such as Merge Sort and Quick Sort.
The code provided implements the Merge Sort algorithm using a divide and conquer strategy. The array is split into halves, sorted, and merged.
Time Complexity: O(n log n) for the average and worst case.
Space Complexity: O(n) due to the temporary arrays used for merging.
This approach involves using two pointers or indices to traverse an array or linked list from two ends towards the center. It’s often applied to solve problems like palindrome checking, two-sum in a sorted array, and finding pairs in a sorted array.
This C code demonstrates using a two-pointer technique in a sorted array to find two numbers that sum up to a target. It initializes pointers at each end of the array and adjusts them till the required sum is found or pointers cross.
Time Complexity: O(n) as each element is examined once in the worst case.
Space Complexity: O(1) because we're only using a fixed amount of additional space.
We can start from the last digit of the array and add each digit of the array to k. Then, divide k by 10, and use the remainder as the current digit's value, with the quotient as the carry. Continue this process until the array is fully traversed and k = 0. Finally, reverse the answer array.
The time complexity is O(n), where n is the length of num. Ignoring the space consumption of the answer array, the space complexity is O(1).
| Approach | Complexity |
|---|---|
| Divide and Conquer | Time Complexity: O(n log n) for the average and worst case. |
| Iterative Two-Pointer Technique | Time Complexity: O(n) as each element is examined once in the worst case. |
| Simulation | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Divide and Conquer (digit + carry propagation) | O(n) | O(1) | When modifying the input array directly and minimizing extra memory |
| Iterative Two-Pointer Technique | O(n) | O(n) | When building the result step-by-step in a new array for simpler implementation |
LeetCode 989 | Add to Array-Form of Integer | Day 7 | 100 Days_LeetCode_Challenge | DSA with edSlash • edSlash • 25,938 views views
Watch 9 more video solutions →Practice Add to Array-Form of Integer with our built-in code editor and test cases.
Practice on FleetCode