🔥🔥Beats 99%||OPTIMIZATION||Two solution||O(2N)->O(N)
Intuition One\n Describe your first thoughts on how to solve this problem. \n1. since half of array contains even and odd numbers, and we have to place even at 
# Intuition One\n<!-- Describe your first thoughts on how to solve this problem. -->\n1. since half of array contains even and odd numbers, and we have to place even at even elements indexes and odd elements at odd indexes so to avoid complexity we declare another vector for even and odd for easy way.\n# Approach Number One\n<!-- Describe your approach to solving the problem. -->\n1. declare even and odd vector. if nums[i] is even array, pushback it in even and if it is odd then in odd array\n2. now, in loop, check whether i is even or odd, accordingly set even elements at even indexes and odd elements at odd indexes respectively.\n3. I have taken k and m so that i can excess even and odd array elements\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(N)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(2N)-> O(N)\n\n# Code One\n```\nclass Solution {\npublic:\n    vector<int> sortArrayByParityII(vector<int>& nums) {\n        vector<int> even;\n        vector<int> odd;\n\n        //seperating even and odd elements\n        for(int i=0; i < nums.size(); i++)\n        {\n            if(nums[i] & 1)\n            odd.push_back(nums[i]);\n            else \n            even.push_back(nums[i]);\n        }\n        int k=0, m=0;\n        inserting even element at even index and odd at odd index.\n        for(int i=0; i < nums.size(); i++)\n        {\n            if(i&1)//odd\n                nums[i]= odd[m++];\n            else//even\n                nums[i]= even[k++];\n        }\n        return nums;\n    }\n};\n```\n# Intuition Two\nWe can solve this without declaring extra arrays, that is, even and odd. We will check directly nums[i] is even or odd and directly place it at its respective index in ans vector.\n\n# Approach Number Two(OPTIMIZED)\n1. iterate throughout the array, check nums[i] is even or odd. \n2. if nums[i] is even set it to even index and if it is odd set it to odd index.\n\n# Time Complexity -> O(N)\n# Space Complexity ->O(1)\nCode block\n```\nclass Solution {\npublic:\n    vector<int> sortArrayByParityII(vector<int>& nums) {\n        vector<int> ans;\n        int k=1, m=0;\n        for(int i=0; i < nums.size(); i++)\n        {\n            if(nums[i] & 1) //odd\n            {\n                ans[k]= nums[i];\n                k=k+2;\n            }\n            else//even\n            {\n                ans[m]=nums[i];\n                m=m+2;\n            }\n        }\n        return ans;\n    }\n};\n// hope you like it. Upvote it please. If you find any problem, do let me know in the comment section.\n
['C++']