c++ optimal easy to understand code
\n\n# Approach\n- In This first we have to make an array named "ans" where we stor the sorted rearranged array.\n- then here we initialized two indexes"posindex
\n\n# Approach\n- In This first we have to make an array named "ans" where we stor the sorted rearranged array.\n- then here we initialized two indexes"posindex=0" which starts from 0 and negindex=1 which start from 1 \n- after iterating if the number is even set it on posindex and increment it by 2 same as for negative index.\n- then at last return the array ans; \n\n<!-- Describe your approach to solving the problem. -->\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\n```\nclass Solution {\npublic:\n    vector<int> sortArrayByParityII(vector<int>& nums) {\n        int n=nums.size();\n        vector<int>ans(n,0);\n        int evenindex=0,oddindex=1;\n        for(int i=0;i<n;i++){\n            if(nums[i]%2==0){\n               ans[evenindex]=nums[i];\n               evenindex+=2;\n            }\n            else{\n               ans[oddindex]=nums[i];\n               oddindex+=2;\n            }\n        }\n        \n        return ans;\n        \n        \n    }\n};\n```
['C++']