✅96.18% Easy solution (Both Approaches) with explanation!
\n# Stack Approach\nWe need to have all even elements to left and all odd elements after it , i.e to right.\n\nTo do this we can either traverse the whole array
\n# Stack Approach\nWe need to have all `even` elements to `left` and all `odd` elements after it , i.e to `right`.\n\nTo do this we can either traverse the whole array 1 time and `push even elements` to our `answer vector` and then again traverse the whole array and then `push odd elements` to our `answer`. This however requires traversal two times.\n\nHow do we do it in 1 go?\n\nWe can make two stacks `even and odd`, which will store all even and odd elements respectively while traversing `nums`. Once the traversing is done, `pop` the elements from even first and push them into `ans` and then `pop` elements from odd and push them into `ans`.\n\nReturn `ans`.\n\n# Stack Approach Code\n```\nclass Solution {\npublic:\n    vector<int> sortArrayByParity(vector<int>& nums) {\n        stack<int> even ,odd;\n        int n = nums.size();\n        for(auto i : nums){\n            if(i&1){\n                odd.push(i);\n            }else{\n                even.push(i);\n            }\n        }\n\n        vector<int> ans;\n        while(!even.empty()){\n            ans.push_back(even.top());\n            even.pop();\n        }\n        while(!odd.empty()){\n            ans.push_back(odd.top());\n            odd.pop();\n        }\n\n        return ans;\n    }\n};\n```\n# Do upvote if it helped you even slightest, someone\'s been downvoting my posts lately.\n\n# Two Pointers Approach\nIn this approach we have two pointers `i pointing to start of array` and `j pointing to end of array`. \n\nWhile `i < j` we increment `i` till `nums[i]` is even and decrement `j` till `nums[j]` is odd, which means after this operation `nums[i]` will be odd and `nums[j]` will be even. But here `i < j` which means even is to right but should be to left ,hence we swap `nums[i] and nums[j]`.\n\nAt last return our original array `nums`.\n\n# Two Pointers Code\n```C++ []\nclass Solution {\npublic:\n    vector<int> sortArrayByParity(std::vector<int>& nums) {\n        int i = 0, j = nums.size() - 1;\n        \n        while (i < j) {\n            while (i < j && nums[i] % 2 == 0)\n                i++;\n            while (i < j && nums[j] % 2 == 1)\n                j--;\n            \n            swap(nums[i], nums[j]);\n        }\n        \n        return nums;\n    }\n};\n```\n```Java []\npublic class Solution {\n    public int[] sortArrayByParity(int[] nums) {\n        int i = 0, j = nums.length - 1;\n        \n        while (i < j) {\n            while (i < j && nums[i] % 2 == 0)\n                i++;\n            while (i < j && nums[j] % 2 == 1)\n                j--;\n            \n            int temp = nums[i];\n            nums[i] = nums[j];\n            nums[j] = temp;\n        }\n        \n        return nums;\n    }\n}\n```\n\n
['Two Pointers', 'Stack', 'Sorting', 'C++', 'Java']