C++ Well Commented Solution [With Explanation] [100%]
\n/* An important observation ---\n1) num%10 gives us the last digit of a number\n2) num = num/10 cuts off the last digit of the number \n3) numVector.back() gi
```\n/* An important observation ---\n1) num%10 gives us the last digit of a number\n2) num = num/10 cuts off the last digit of the number \n3) numVector.back() gives us the last digit of the number in vector form\n4) numVector.pop_back() cuts off the last digit of the number in vector form\n5) The extra space required can be reduced by overwriting the first vector. \n*/\n\n\nclass Solution\n{\npublic:\n    vector<int> addToArrayForm(vector<int>& a, int k);\n};\n\n/* Returns the sum of 2 numbers in vector form */\nvector<int> Solution :: addToArrayForm(vector<int>& a, int k)\n{\n    // Get the length of the first number\n    int n = a.size();\n    \n    // Vector to store the answer\n    vector<int> answer;\n    \n    /* Start adding both the numbers from the end */\n    \n    int carry = 0;\n    // As long as one of the number exists, keep adding them\n    while(!a.empty() || k!=0)\n    {\n        // Get the last digits of both the numbers. If a vector has finished off, the last digit is zero\n        int lastDigit_1 = a.empty() ? 0 : a.back();\n        int lastDigit_2 = k%10;\n        \n        // Sum up the digits and add the carry\n        int sum = lastDigit_1 + lastDigit_2 + carry;\n        answer.push_back(sum%10);\n        carry = sum/10;\n        \n        // Remove the last digits of both the numbers\n        if(!a.empty()) a.pop_back();\n        k = k/10;\n    }\n    \n    // If the carry is remaining, add it\n    if(carry!=0) answer.push_back(carry);\n    \n    // Reverse the answer, since we were summing up from the end\n    reverse(answer.begin(), answer.end());\n    \n    // Return the answer in vector format\n    return answer;\n}\n```
[]