Add to Array-Form of Integer - Complete Solution Guide
Add to Array-Form of Integer is LeetCode problem 989, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
The array-form of an integer num is an array representing its digits in left to right order. For example, for 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] Expl
Detailed Explanation
The problem asks us to add an integer `k` to an integer represented as an array of digits `num`. The `num` array represents the number in array form, meaning each digit is stored in the array from left to right (most significant to least significant digit). We need to return the array form of the sum of `num` and `k`. For instance, if `num = [1, 2, 0, 0]` and `k = 34`, we treat `num` as the integer 1200. The sum is 1200 + 34 = 1234. The desired output is the array form of 1234, which is `[1, 2, 3, 4]`. The constraints are that the array `num` has between 1 and 10000 elements, each element is a digit between 0 and 9, `num` doesn't have leading zeros (except if the number is 0), and `k` is between 1 and 10000.
Solution Approach
The solution simulates the manual addition process. We iterate through the array `num` from right to left (least significant digit to most significant digit) and simultaneously process the digits of `k` (also from right to left). At each step, we add the corresponding digit from `num`, the last digit of `k`, and the carry-over from the previous addition. The sum's last digit becomes part of the result, and the carry-over is updated. We continue this process until we've processed all digits of `num`, all digits of `k`, and there's no carry-over left.
Step-by-Step Algorithm
- Step 1: Initialize an empty list (or array) to store the result.
- Step 2: Initialize a carry variable to 0.
- Step 3: Iterate from the end of the `num` array towards the beginning (right to left).
- Step 4: In each iteration, extract the current digit from `num` (if there are digits left in `num`, otherwise, use 0).
- Step 5: Extract the last digit from `k` (if `k` is greater than 0, otherwise, use 0). This can be done by taking `k % 10`.
- Step 6: Add the digit from `num`, the last digit of `k`, and the carry together.
- Step 7: Append the last digit of the sum (sum % 10) to the beginning of the result list.
- Step 8: Update the carry by taking the integer division of the sum by 10 (sum // 10).
- Step 9: Divide `k` by 10 (integer division) to move to the next digit of `k`.
- Step 10: Repeat steps 4-9 until all digits in `num` have been processed, `k` is 0, and there is no remaining carry.
- Step 11: If there's a remaining carry after the loop, append it to the beginning of the result list.
- Step 12: Return the result list.
Key Insights
- Insight 1: Simulating addition like we do it by hand, starting from the least significant digit (rightmost part of the array) is crucial.
- Insight 2: Handling the carry-over from each digit addition is essential for obtaining the correct sum.
- Insight 3: We need to account for cases where `k` has more digits than `num` or vice versa, and for any remaining carry after processing both `num` and `k`.
Complexity Analysis
Time Complexity: O(max(n, log(k)))
Space Complexity: O(max(n, log(k)))
Topics
This problem involves: Array, Math.
Companies
Asked at: Avito, ByteDance, Zoho.