100% User Beats of C++ || Step By Step Explain || Fast Solution || Easy to Understand ||
Abhiraj Pratap Singh\n\n---\n\n# UPVOTE IT....\n\n---\n\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \n- The problem seems to inv
# Abhiraj Pratap Singh\n\n---\n\n# UPVOTE IT....\n\n---\n\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- The problem seems to involve adding one to a given integer represented as an array of digits. The array may contain leading zeros, and the goal is to return the result as a new array.\n\n---\n\n![Screenshot (47).png](https://assets.leetcode.com/users/images/ab79d6a6-0341-40be-8334-ffb064956f23_1704901894.8121877.png)\n\n\n---\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Start iterating from the rightmost digit of the array.\n2. Increment the rightmost digit by 1.\n3. If the incremented digit becomes 10, set it to 0 and move to the next left digit. Continue this process until a digit is found that is less than 10.\n4. If all digits are incremented and become 10, a new digit 1 should be added at the beginning of the array.\n5. Return the modified array.\n\n---\n\n\n# Complexity\n\n---\n\n\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n- **Time complexity: The time complexity is O(n),** where n is the number of digits in the array. In the worst case, the algorithm iterates through all the digits once.\n\n---\n\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n- **Space complexity: The space complexity is O(1),** as the algorithm uses only a constant amount of extra space regardless of the input size. The modification is done in place on the given vector.\n\n---\n\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int> plusOne(vector<int>& digits) \n    {\n         int n = digits.size();\n        for(int i = n-1; i >= 0; i--){\n            if(i == n-1)\n                digits[i]++;\n            if(digits[i] == 10){\n                digits[i] = 0;\n                if(i != 0){\n                    digits[i-1]++;\n                }\n                else{\n                    digits.push_back(0);\n                    digits[i] = 1;\n                }\n            }\n        }\n        return digits;\n    }\n};\n```\n\n\n---\n\n# if you like the solution please UPVOTE it....\n\n---\n\n![discord-discordgifemoji.gif](https://assets.leetcode.com/users/images/55f15baf-5a22-4bc6-abc1-385ca9e34de3_1704902161.1924372.gif)\n\n---\n\n\n![fucking-die-redditor-stupid-cuck-funny-cat.gif](https://assets.leetcode.com/users/images/7f12a62d-355b-41be-8a7a-9b55973e59ed_1704902166.0749838.gif)\n\n---\n\n\n
['Array', 'Math', 'C++']