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 <= 104The 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.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Digit-by-digit addition with carry | O(n + log k) | O(1) extra (excluding output) |
edSlash
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.
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}The code provided implements the Merge Sort algorithm using a divide and conquer strategy. The array is split into halves, sorted, and merged.
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.
1
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
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.
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.
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.
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.
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.