Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

989. Add to Array-Form of Integer

Easy45.0% Acceptance
ArrayMath
Asked by:
F
Facebook
ProblemSolutions (12)VideosCompanies (1)Notes

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
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions

Approach

The key idea in #989 Add to Array-Form of Integer is to treat the array as a large integer where each element represents a digit. Instead of converting the entire array to a number (which may overflow), simulate the addition process digit by digit, similar to how we perform manual addition.

Start from the last digit of the array and add the integer k. Keep track of the carry generated after each addition. Update the current digit using modulo (% 10) and propagate the carry to the next position. Continue this process while there are remaining digits in the array or a remaining carry from k.

If the addition produces extra digits at the front, prepend them to the result. This approach efficiently avoids large integer conversions and works well for long arrays. The algorithm runs in linear time relative to the number of digits processed and uses minimal additional space.

Complexity

ApproachTime ComplexitySpace Complexity
Digit-by-digit addition with carryO(n + log k)O(1) extra (excluding output)

Video Solution Available

edSlash

View all video solutions

Solutions (12)

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.

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

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2
3void merge(int arr[], int l, int m, int r) {
4    int i, j, k;
5    int n1 = m - l + 1;
6    int n2 =  r - m;
7    int L[n1], R[n2];
8    for (i = 0; i < n1; i++)
9        L[i] = arr[l + i];
10    for (j = 0; j < n2; j++)
11        R[j] = arr[m + 1+ j];
12    i = 0; j = 0; k = l;
13    while (i < n1 && j < n2) {
14        if (L[i] <= R[j]) {
15            arr[k] = L[i];
16            i++;
17        } else {
18            arr[k] = R[j];
19            j++;
20        }
21        k++;
22    }
23    while (i < n1) {
24        arr[k] = L[i];
25        i++;
26        k++;
27    }
28    while (j < n2) {
29        arr[k] = R[j];
30        j++;
31        k++;
32    }
33}
34
35void mergeSort(int arr[], int l, int r) {
36    if (l < r) {
37        int m = l+(r-l)/2;
38        mergeSort(arr, l, m);
39        mergeSort(arr, m+1, r);
40        merge(arr, l, m, r);
41    }
42}
43
44void printArray(int A[], int size) {
45    int i;
46    for (i=0; i < size; i++)
47        printf("%d ", A[i]);
48    printf("\n");
49}
50
51int main() {
52    int arr[] = {12, 11, 13, 5, 6, 7};
53    int arr_size = sizeof(arr)/sizeof(arr[0]);
54
55    printf("Given array is \n");
56    printArray(arr, arr_size);
57
58    mergeSort(arr, 0, arr_size - 1);
59
60    printf("Sorted array is \n");
61    printArray(arr, arr_size);
62    return 0;
63}

Explanation

The code provided implements the Merge Sort algorithm using a divide and conquer strategy. The array is split into halves, sorted, and merged.

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.

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.

CC++JavaPythonC#JavaScript
1

Video Solutions

Watch expert explanations and walkthroughs

LeetCode 989 | Add to Array-Form of Integer | Day 7 | 100 Days_LeetCode_Challenge | DSA with edSlash

edSlash
22:1311,334 views

Asked By Companies

1 companies
F
Facebook

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Rotate ImageMedium
Plus OneEasy
Max Points on a LineHard
Evaluate Reverse Polish NotationMedium
More similar problems

Related Topics

ArrayMath

Problem Stats

Acceptance Rate45.0%
DifficultyEasy
Companies1

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Why not convert the array to an integer first?

Converting the entire array into an integer may cause overflow when the number of digits is very large. Processing digits individually ensures the solution works efficiently even for large inputs.

Is Add to Array-Form of Integer asked in FAANG interviews?

Yes, this type of problem can appear in technical interviews because it tests understanding of arrays, digit manipulation, and handling carry operations. It also evaluates how well candidates avoid overflow by working with digits directly.

What data structure is best for Add to Array-Form of Integer?

An array or dynamic list is sufficient for this problem since each element represents a digit. The array structure allows easy traversal from the end and direct updates while handling carry during addition.

What is the optimal approach for Add to Array-Form of Integer?

The optimal approach simulates manual addition from the last digit of the array while adding the integer k. By maintaining a carry and updating digits one by one, we avoid integer overflow and achieve linear time complexity.

#
include
<stdio.h>
2
#
include
<stdlib.h>
3
4
void
twoSum
(
int
*
nums
,
int
numsSize
,
int
target
,
int
*
result
)
{
5
int
left
=
0
,
right
=
numsSize
-
1
;
6
while
(
left
<
right
)
{
7
int
sum
=
nums
[
left
]
+
nums
[
right
]
;
8
if
(
sum
==
target
)
{
9
result
[
0
]
=
left
;
10
result
[
1
]
=
right
;
11
return
;
12
}
else
if
(
sum
<
target
)
{
13
left
++
;
14
}
else
{
15
right
--
;
16
}
17
}
18
result
[
0
]
=
-
1
;
19
result
[
1
]
=
-
1
;
20
}
21
22
int
main
(
)
{
23
int
nums
[
]
=
{
2
,
3
,
4
,
5
,
6
,
7
}
;
24
int
target
=
9
;
25
int
result
[
2
]
;
26
twoSum
(
nums
,
6
,
target
,
result
)
;
27
printf
(
"Indices: %d, %d\n"
,
result
[
0
]
,
result
[
1
]
)
;
28
return
0
;
29
}

Explanation

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.