Day 46 || C++ || Easiest Beginner Friendly Sol || O(max(N, log K)) time and O(max(N, log K)) space
Intuition of this Problem:\n Describe your first thoughts on how to solve this problem. \nNOTE - PLEASE READ APPROACH FIRST THEN SEE THE CODE. YOU WILL DEFINITE
# Intuition of this Problem:\n<!-- Describe your first thoughts on how to solve this problem. -->\n**NOTE - PLEASE READ APPROACH FIRST THEN SEE THE CODE. YOU WILL DEFINITELY UNDERSTAND THE CODE LINE BY LINE AFTER SEEING THE APPROACH.**\n\n# Approach for this Problem:\n1. Initialize an empty vector "ans" to store the final result\n2. Initialize "carry" to 0 and "i" to the index of the last digit of "num"\n3. Loop while there is a digit left in k, or there are digits left in num, or there is a carry value.\n4. Compute the sum by adding the carry, the last digit of k (if k has digits left) and the i-th digit of num (if num has digits left).\n5. Compute the carry by taking the sum divided by 10.\n6. Add the digit to the front of the "ans" vector by taking the sum modulo 10.\n7. Decrement i and divide k by 10 (if applicable).\n8. Reverse the "ans" vector and return it.\n<!-- Describe your approach to solving the problem. -->\n\n# Humble Request:\n- If my solution is helpful to you then please **UPVOTE** my solution, your **UPVOTE** motivates me to post such kind of solution.\n- Please let me know in comments if there is need to do any improvement in my approach, code....anything.\n- **Let\'s connect on** https://www.linkedin.com/in/abhinash-singh-1b851b188\n\n![57jfh9.jpg](https://assets.leetcode.com/users/images/c2826b72-fb1c-464c-9f95-d9e578abcaf3_1674104075.4732099.jpeg)\n\n# Code:\n```C++ []\nclass Solution {\npublic:\n    vector<int> addToArrayForm(vector<int>& num, int k) {\n        vector<int> ans;\n        int n = num.size();\n        int carry = 0, i = n-1;\n        while (k > 0 || i >= 0 || carry > 0) {\n            int sum = carry;\n            if (k > 0) {\n                int remainder = k % 10;\n                sum += remainder;\n                k = k / 10;\n            }\n            if (i >= 0) {\n                sum += num[i];\n                i--;\n            }\n            carry = sum / 10;\n            ans.push_back(sum % 10);\n        }\n        reverse(ans.begin(), ans.end());\n        return ans;\n    }\n};\n```\n```Java []\nclass Solution {\n    public List<Integer> addToArrayForm(int[] num, int k) {\n        List<Integer> ans = new ArrayList<>();\n        int n = num.length;\n        int carry = 0, i = n-1;\n        while (k > 0 || i >= 0 || carry > 0) {\n            int sum = carry;\n            if (k > 0) {\n                int remainder = k % 10;\n                sum += remainder;\n                k = k / 10;\n            }\n            if (i >= 0) {\n                sum += num[i];\n                i--;\n            }\n            carry = sum / 10;\n            ans.add(0, sum % 10);\n        }\n        return ans;\n    }\n}\n\n```\n```Python []\nclass Solution:\n    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n        ans = []\n        n = len(num)\n        carry = 0\n        i = n - 1\n        while k > 0 or i >= 0 or carry > 0:\n            sum = carry\n            if k > 0:\n                remainder = k % 10\n                sum += remainder\n                k //= 10\n            if i >= 0:\n                sum += num[i]\n                i -= 1\n            carry = sum // 10\n            ans.insert(0, sum % 10)\n        return ans\n\n```\n\n# Time Complexity and Space Complexity:\n- Time complexity: **O(max(N, log K))**, where N is the number of digits in "num" and K is the value of "k". We have to iterate over all digits in both "num" and "k" to compute the sum.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: **O(max(N, log K))**, since the size of the output vector can be at most max(N, log K) + 1.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->
['Array', 'Math', 'C++', 'Java', 'Python3']