Add an Integer to a Number Represented as Decimal Digits

Quick Overview

Add an Integer to a Number Represented as Decimal Digits. The array num stores a nonnegative integer's decimal digits from most significant to least significant.

Add an Integer to a Number Represented as Decimal Digits

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem The array `num` stores a nonnegative integer's decimal digits from most significant to least significant. Return the decimal digit array for that integer plus k. The input number may be too long for a fixed-width integer. ```cpp class Solution { public: vector<int> addToArrayForm(vector<int>& num, int k); }; ``` ### Examples **Example 1:** ```text Input: num = [9,9], k = 5 Output: [1,0,4] Explanation: 99+5=104, adding a new most-significant digit. ``` **Example 2:** ```text Input: num = [0], k = 123 Output: [1,2,3] Explanation: Zero plus123 has exactly those three digits. ``` ### Constraints - `1 <= num.length <= 10^4`. - `0 <= num[i] <= 9`. - There are no leading zeros except the number zero itself. - `1 <= k <= 10^4`. ### Contract considerations All array entries are individual decimal digits. k is positive under the source bounds. ```hint Read the boundary condition Treat k as the initial carry rather than trying to convert the large digit array to one machine integer. ``` ### Boundary reminders All array entries are individual decimal digits. k is positive under the source bounds.

Overview: Add an Integer to a Number Represented as Decimal Digits. The array num stores a nonnegative integer's decimal digits from most significant to least significant.

Google logo
Google
Jun 22, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

The array num stores a nonnegative integer's decimal digits from most significant to least significant. Return the decimal digit array for that integer plus k. The input number may be too long for a fixed-width integer.

class Solution {
public:
    vector<int> addToArrayForm(vector<int>& num, int k);
};

Examples

Example 1:

Input: num = [9,9], k = 5
Output: [1,0,4]
Explanation: 99+5=104, adding a new most-significant digit.

Example 2:

Input: num = [0], k = 123
Output: [1,2,3]
Explanation: Zero plus123 has exactly those three digits.

Constraints

  • 1 <= num.length <= 10^4 .
  • 0 <= num[i] <= 9 .
  • There are no leading zeros except the number zero itself.
  • 1 <= k <= 10^4 .

Contract considerations

All array entries are individual decimal digits. k is positive under the source bounds.

Boundary reminders

All array entries are individual decimal digits. k is positive under the source bounds.