<p>The <strong>array-form</strong> of an integer <code>num</code> is an array representing its digits in left to right order.</p>

<ul>
	<li>For example, for <code>num = 1321</code>, the array form is <code>[1,3,2,1]</code>.</li>
</ul>

<p>Given <code>num</code>, the <strong>array-form</strong> of an integer, and an integer <code>k</code>, return <em>the <strong>array-form</strong> of the integer</em> <code>num + k</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> num = [1,2,0,0], k = 34
<strong>Output:</strong> [1,2,3,4]
<strong>Explanation:</strong> 1200 + 34 = 1234
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> num = [2,7,4], k = 181
<strong>Output:</strong> [4,5,5]
<strong>Explanation:</strong> 274 + 181 = 455
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> num = [2,1,5], k = 806
<strong>Output:</strong> [1,0,2,1]
<strong>Explanation:</strong> 215 + 806 = 1021
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= num.length &lt;= 10<sup>4</sup></code></li>
	<li><code>0 &lt;= num[i] &lt;= 9</code></li>
	<li><code>num</code> does not contain any leading zeros except for the zero itself.</li>
	<li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution {
public:
    vector<int> addToArrayForm(vector<int>& num, int k) {
        vector<int> kl;
        while (k>0)
        {
            kl.push_back(k%10);
            k = k/10;
        }
        int l1 = num.size();
        int l2 = kl.size();
        
        int idx1 = l1-1;
        int idx2 = 0;
        int carry = 0;
        vector<int> ans;
        while (true)
        {
            if (idx1<0 && idx2>=l2)
            {
                if (carry>0)
                    ans.push_back(1);
                break;
            }
            int d = 0;
            
            if (idx1<0)
            {
                d = kl[idx2] + carry;
                idx2++;
            }
            else if (idx2>=l2)
            {
                d = num[idx1] + carry;
                idx1--;
            }
            else
            {
                d = num[idx1] + kl[idx2] + carry;
                idx1--;
                idx2++;
            }
            
            if (d>9)
            {
                d -= 10;
                carry = 1;
            }
            else carry = 0;
            ans.push_back(d);
        }
        
        int l3 = ans.size();
        for (int i=0;i<l3/2;i++)
        {
            int temp = ans[i];
            ans[i] = ans[l3-i-1];
            ans[l3-i-1] = temp;
        }
        return ans;
    }
    
    
};
31600