Beats 100% of users with C++ || Step By Step Explain || Easy To Understand ||
Abhiraj Pratap Singh\n\n---\n\n# if you like the solution please UPVOTE it....\n\n---\n\n# Intuition\n Describe your first thoughts on how to solve this problem
# Abhiraj Pratap Singh\n\n---\n\n# if you like the solution please UPVOTE it....\n\n---\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- The problem seems to be asking for separating the digits of each number in the input vector and returning them as individual elements in a new vector.\n\n---\n\n![Screenshot (47).png](https://assets.leetcode.com/users/images/de9d9642-64cb-480a-a706-26d36d2fc0b6_1708768089.5348318.png)\n\n\n---\n# Approach 1\n<!-- Describe your approach to solving the problem. -->\n- Iterate through each number in the input vector.\n- Convert each number to a string using to_string() function.\n- Iterate through the characters of the string representation of the number.\n- Push each digit (converted to integer) into the answer vector.\n- Return the answer vector.\n\n---\n\n# Code 1\n```\nclass Solution {\npublic:\n    vector<int> separateDigits(vector<int>& nums) \n    {\n        vector<int>ans;\n        for( int i = 0 ; i < nums.size() ; i++ )\n        {\n            string temp = to_string ( nums[i] ) ;\n            for ( int j = 0 ; j < temp.length() ; j++ )\n                ans.push_back( temp[j] - \'0\' );\n        }\n        return ans ;\n    }\n};\n```\n# Complexity\n\n- **Time complexity:** O(n\u22C5m), where n is the number of elements in the input vector and m is the average number of digits in each element.\n\n- **Space complexity:** O(n\u22C5m) since we are storing each digit separately in the answer vector.\n\n---\n\n# Approach 2 \n\n- Iterate through each number in the input vector from right to left.\n- Extract each digit of the current number by repeatedly taking modulo 10 and dividing by 10.\n- Push each digit into the answer vector.\n- After processing all numbers, reverse the answer vector to maintain the original order of digits.\n- Return the answer vector.\n\n---\n\n# Code 2\n```\nclass Solution {\npublic:\n    vector<int> separateDigits(vector<int>& nums) \n    {\n        vector<int>ans;\n        for( int i = nums.size() - 1 ; i >= 0 ; i-- )\n        {\n            int temp = nums[i] ;\n            while( temp )\n            {\n                ans.push_back( temp % 10 ) ;\n                temp = temp / 10 ;\n            }\n        }\n        reverse( ans.begin() , ans.end() ) ;\n        return ans ;\n    }\n};\n```\n# Complexity \n\n- **Time complexity:** O(n\u22C5m), where n is the number of elements in the input vector and m is the average number of digits in each element.\n\n- **Space complexity:** O(n\u22C5m) since we are storing each digit separately in the answer vector.\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/ea9a28df-f77e-433f-b526-b14178f5f461_1708768001.090222.gif)\n\n![fucking-die-redditor-stupid-cuck-funny-cat.gif](https://assets.leetcode.com/users/images/0cb98da9-4e6f-462a-8586-bb03eb96ac6f_1708768005.854289.gif)\n\n---\n\n
['Array', 'Simulation', 'C++']