Add to Array-Form of Integer
EASYDescription
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] Explanation: 215 + 806 = 1021
Constraints:
1 <= num.length <= 1040 <= num[i] <= 9numdoes not contain any leading zeros except for the zero itself.1 <= k <= 104
Approaches
Checkout 2 different approaches to solve Add to Array-Form of Integer. Click on different approaches to view the approach and algorithm in detail.
Schoolbook Addition
This approach simulates the manual, pencil-and-paper method of adding two numbers. It processes the numbers from right to left (least significant digit to most significant), keeping track of a carry value at each step.
Algorithm
- Initialize an empty
LinkedListfor the result. - Initialize a pointer
ito the last index ofnum. - Loop as long as
iis valid (>= 0) ork > 0. - Inside the loop, if
iis valid, addnum[i]tok. - Add the last digit of the current sum (
k % 10) to the front of the result list. - Update
kto be the carry (k / 10). - Decrement
i. - Return the result list.
This is the most efficient way to solve the problem as it performs the addition in a single pass without any expensive type conversions or large object allocations. The algorithm iterates through the num array from right to left, adding the digits of k and any carry from the previous step. The integer k itself is used to carry over the sum to the next higher-order digit.
Algorithm Steps:
- Initialize a
LinkedListto store the result digits. ALinkedListis chosen for its efficient O(1)addFirstoperation, which avoids a final reversal step. - Start a loop that continues as long as there are digits left in
numto process ork(which holds the carry and remaining part of the number to be added) is greater than 0. - In each iteration, if there's a digit in
numat the current position, add it tok. - The current digit for the result is
k % 10. - The new carry is
k / 10. We updatekto this value for the next iteration. - Prepend the current digit (
k % 10) to the result list. - Move to the next digit in
numby decrementing the pointer. - After the loop, the
LinkedListcontains the digits of the sum in the correct order. Return it.
import java.util.LinkedList;
import java.util.List;
class Solution {
public List<Integer> addToArrayForm(int[] num, int k) {
LinkedList<Integer> result = new LinkedList<>();
int i = num.length - 1;
while (i >= 0 || k > 0) {
// Add the current digit of num if it exists
if (i >= 0) {
k += num[i];
i--;
}
// The current digit of the sum is k % 10
result.addFirst(k % 10);
// The carry is k / 10
k /= 10;
}
return result;
}
}
Complexity Analysis
Pros and Cons
- Highly efficient with optimal time and space complexity.
- Avoids intermediate data structures like strings or
BigIntegerobjects, reducing overhead. - Demonstrates a fundamental understanding of arithmetic operations.
- Slightly more complex to implement correctly compared to the
BigIntegerapproach.
Code Solutions
Checking out 3 solutions in different languages for Add to Array-Form of Integer. Click on different languages to view the code.
class Solution {
public
List<Integer> addToArrayForm(int[] num, int k) {
int i = num.length - 1, carry = 0;
LinkedList<Integer> ans = new LinkedList<>();
while (i >= 0 || k > 0 || carry > 0) {
carry += (i < 0 ? 0 : num[i--]) + k % 10;
ans.addFirst(carry % 10);
carry /= 10;
k /= 10;
}
return ans;
}
}
Video Solution
Watch the video walkthrough for Add to Array-Form of Integer
Similar Questions
5 related questions you might find useful
Patterns:
Data Structures:
Companies:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.