Add to Array-Form of Integer - Problem

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.

Input & Output

Example 1 — Basic Addition
$ Input: num = [1,2,0,0], k = 34
Output: [1,2,3,4]
💡 Note: Array [1,2,0,0] represents 1200. Adding 34 gives us 1234, which is [1,2,3,4] in array form.
Example 2 — Small Array
$ Input: num = [2,7,4], k = 181
Output: [4,5,5]
💡 Note: Array [2,7,4] represents 274. Adding 181 gives us 455, which is [4,5,5] in array form.
Example 3 — Carry Overflow
$ Input: num = [9,9,9], k = 1
Output: [1,0,0,0]
💡 Note: Array [9,9,9] represents 999. Adding 1 gives us 1000, which requires expanding to [1,0,0,0].

Constraints

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

Visualization

Tap to expand
Add to Array-Form: [1,2,0,0] + 34Input Array:1200Represents: 1200+34k = 34Addition with CarryResult Array:1234= 1200 + 34 = 1234
Understanding the Visualization
1
Input
Array [1,2,0,0] represents 1200, k = 34
2
Process
Add k to array using carry propagation
3
Output
Result [1,2,3,4] represents 1234
Key Takeaway
🎯 Key Insight: We can add directly to the array using carry propagation, avoiding integer overflow for large numbers
Asked in
Google 15 Apple 12 Microsoft 8
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen