Solution
C++ []\nclass Solution {\npublic:\n    vector<int> sortArrayByParityII(vector<int>& nums) {\n        int n=nums.size();\n        int i=0;\n        int j=1;\n   
```C++ []\nclass Solution {\npublic:\n    vector<int> sortArrayByParityII(vector<int>& nums) {\n        int n=nums.size();\n        int i=0;\n        int j=1;\n        while(i<n && j<n){\n            if(nums[i]%2==0)\n                i=i+2;\n            else if(nums[j]%2==1)\n                j=j+2;\n            else\n                swap(nums[i],nums[j]);\n        }\n        return nums;\n    }\n};\n```\n\n```Python3 []\nclass Solution:\n    def sortArrayByParityII(self, nums: List[int]) -> List[int]:\n\n        j=1\n\n        for i in range(0,len(nums),2):\n            if nums[i]%2:\n                while nums[j]%2:\n                    j+=2\n                nums[i],nums[j]=nums[j],nums[i]\n\n        return nums\n```\n\n```Java []\nclass Solution {\n    public int[] sortArrayByParityII(int[] nums) {\n        int[] freq = new int[1001];\n        for (int n : nums){\n            freq[n]++;\n        }\n        for (int k = 0; k < 2; k++) {\n            int cur = k;\n            for (int i = k; i < nums.length; i += 2) {\n                while (freq[cur] == 0) cur += 2;\n                freq[cur]--;\n                nums[i] = cur;\n            }\n        }\n        return nums;\n    }\n}\n```\n
['C++', 'Java', 'Python3']