🚀 C++, 100% ! 🎉 3 Easy and Straightforward Approaches + [animation]
Intuition\n\n\n\n\n# Approach 1\n1. Create an empty vector called result to store the sorted array.\n\n1. Iterate through the elements of the input vector nums 
# Intuition\n![ezgif.com-video-to-gif (4).gif](https://assets.leetcode.com/users/images/db4248ba-847f-46b2-aab9-60e955a48de5_1695862918.8347442.gif)\n\n\n\n# Approach 1\n1. Create an empty vector called result to store the sorted array.\n\n1. Iterate through the elements of the input vector nums using a for loop.\n\n1. For each element nums[i], check if it is even or odd by using the modulus operator %. If the element is even (nums[i] % 2 == 0), it is added to the result vector using the push_back function.\n\n1. After the first loop completes, iterate through the elements of the input vector nums again using another for loop.\n\n1. For each element nums[i], check if it is odd (nums[i] % 2 == 1). If the element is odd, it is added to the result vector using the push_back function.\n\n1. Finally, return the result vector, which contains the even numbers followed by the odd numbers.\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code 1\n```\nclass Solution {\npublic:\n    vector<int> sortArrayByParity(vector<int>& nums) {\n        vector<int> result;\n        for(int i = 0; i < nums.size(); i++) if(nums[i] % 2 == 0) result.push_back(nums[i]);\n        for(int i = 0; i < nums.size(); i++) if(nums[i] % 2 == 1) result.push_back(nums[i]);\n        return result;\n    }\n};\n```\n\n---\n---\n\n\n\n# Intuition\n![ezgif.com-video-to-gif (5).gif](https://assets.leetcode.com/users/images/48780d40-4ae8-4a51-9237-1f61fa6b9d63_1695863778.3541245.gif)\n\n\n\n# Approach 2\n1. Create two pointers, left and right, initialized to the start and end indices of the nums vector, respectively.\n\n1. Create a new vector called result with the same size as the input vector nums. This vector will store the sorted array.\n\n1. Iterate through each element num in the nums vector using a range-based for loop.\n\n1. For each element, check if it is even or odd by using the modulus operator %. \n    1. If the element is even (num % 2 == 0), store it at the index left in the result vector and increment left by 1. This ensures that even numbers are placed at the beginning of the result vector.\n\n    1. If the element is odd (num % 2 == 1), store it at the index right in the result vector and decrement right by 1. This ensures that odd numbers are placed at the end of the result vector.\n\n\n1. Finally, return the result vector, which now contains the even numbers followed by the odd numbers.\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code 2 \n```\nclass Solution {\npublic:\n    vector<int> sortArrayByParity(vector<int>& nums) {\n        int left = 0;\n        int right = nums.size() - 1;\n        vector<int> result(nums.size());\n\n       for(int num: nums){\n           if(num % 2 == 0) {\n               result[left] = num;\n               left++;\n           }\n           else {\n               result[right] = num;\n               right --;\n           }\n       }\n        return result;\n    }\n};\n```\n\n\n---\n---\n\n\n\n# Intuition\n![ezgif.com-video-to-gif (5).gif](https://assets.leetcode.com/users/images/724f06b7-7d28-4d8e-9abe-e57c6e383735_1695869018.9710197.gif)\n\n\n# Approach 3\n\n1. Initialize two variables, left and right, to keep track of the indices in the vector. left is set to 0, representing the leftmost element, and right is set to nums.size() - 1, representing the rightmost element.\n\n1. Enter a loop that continues until left is greater than or equal to right.\n\n   1. If the number is even, increment the left index by 1 to move to the next element.\n\n    1. If the number is odd \n        1. swap the number at index left with the number at index right in the vector nums. This is done using the swap function.\n\n        1. decrement the right index by 1 to move to the previous element.\n\n\n1. Once the loop exits, the vector nums is modified in place such that all even numbers are positioned at the beginning, followed by the odd numbers. return it.\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code 3 \n```\nclass Solution {\npublic:\n    vector<int> sortArrayByParity(vector<int>& nums) {\n        int left = 0;\n        int right = nums.size() - 1;\n\n        while (left < right) {\n            if (nums[left] % 2 == 0) {\n                left++;  // Move the left pointer if the number is even\n            } else {\n                swap(nums[left], nums[right]);  // Swap odd number with the number at the right pointer\n                right--;  // Move the right pointer\n            }\n        }\n\n        return nums;\n    }\n};\n```\n\nhttps://youtu.be/JcLlpv9FAS4
['C++']