Skip to main content

Add to Array-Form of Integer - Solution & Explanation

EasyArrayMath18 min readAsked at: Amazon, Meta, Google +1
Practice this problem

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]
Explanation: 215 + 806 = 1021

 

Constraints:

  • 1 <= num.length <= 104
  • 0 <= num[i] <= 9
  • num does not contain any leading zeros except for the zero itself.
  • 1 <= k <= 104

Approach Overview

Problem 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.

Approach 1: Divide and Conquer

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n log n) for the average and worst case.
Space Complexity: O(n) due to the temporary arrays used for merging.

Try this approach in the editor →

Approach 2: Iterative Two-Pointer Technique

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor →

Approach 3: Simulation

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).

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Divide and Conquer

Time Complexity: O(n log n) for the average and worst case.
Space Complexity: O(n) due to the temporary arrays used for merging.

Iterative Two-Pointer Technique

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.

Simulation

Detailed Complexity Analysis

ApproachTimeSpaceWhen 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 TechniqueO(n)O(n)When building the result step-by-step in a new array for simpler implementation

Video Solution

LeetCode 989 | Add to Array-Form of Integer | Day 7 | 100 Days_LeetCode_Challenge | DSA with edSlashedSlash29,261 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Add to Array-Form of Integer easy or hard?
Add to Array-Form of Integer is classified as an Easy problem on LeetCode. The challenge mainly involves simulating manual addition using arrays and handling carry correctly. Developers comfortable with array traversal and basic math typically solve it in one pass.
Add to Array-Form of Integer Python/Java solution
Python and Java implementations follow the same logic: iterate from the end of the array, add digits with k and carry, update the current position or append to a result list, and continue until all digits are processed. Both languages achieve O(n) time complexity with straightforward arithmetic operations.
How to solve Add to Array-Form of Integer in O(n)?
Start from the last digit of the array and repeatedly add the last digit of k and any carry. Store the resulting digit using modulo 10 and update the carry using integer division. Continue until both the array digits and k are fully processed, then handle any remaining carry. This single pass ensures O(n) complexity.
What is the best approach for Add to Array-Form of Integer?
The best approach is digit-by-digit addition from the end of the array while maintaining a carry, similar to manual arithmetic. Iterate from the last index, add the current digit with k % 10 and carry, then update k using k // 10. This method runs in O(n) time and uses O(1) extra space if the input array is modified in place.
Is Add to Array-Form of Integer asked at Google/Amazon/Meta?
Array-based arithmetic and digit manipulation problems appear frequently in interviews at companies like Google, Amazon, and Meta. Variants include adding numbers stored in arrays or linked lists and managing carry propagation. The problem tests understanding of array traversal and basic math operations.
What data structure is used in Add to Array-Form of Integer?
The primary data structure is an array that stores each digit of the integer. The algorithm performs sequential traversal and updates digits while maintaining a carry value. Some implementations also use a dynamic list to build the result before reversing it.
What is the time complexity of Add to Array-Form of Integer?
The optimal solution runs in O(n) time where n is the number of digits in the array representation. Each digit is processed once while propagating the carry. Space complexity can be O(1) for in-place modification or O(n) if constructing a separate result array.

Ready to solve this problem?

Practice Add to Array-Form of Integer with our built-in code editor and test cases.

Practice on FleetCode