Easiest C++ Code (2 Pointer) (Fully Explained)(O(n) Solution)
Intuition\n Describe your first thoughts on how to solve this problem. \nIn this problem we use 2 pointers (i,j) pointing at start and end of array .\n\nStart a
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIn this problem we use 2 pointers (i,j) pointing at start and end of array .\n\nStart a while loop which runs if i<j only.\nThere  are 3 conditions:\n\n1-> If a[i] is odd and a[j] is even then swap them and increment i and decrement j.\n2->Else If a[i] is even increment the i pointer.\n3->Else decrement the j pointer.\n\n# Approach\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(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int> sortArrayByParity(vector<int>& a) {\n        int i=0,j=a.size()-1,k,c=0;\n\n        while(i<j){\n            if(a[i]%2==1 and a[j]%2==0){\n                swap(a[i],a[j]);\n                i++;\n                j--;\n            } else if(a[i]%2==0)\n            i++; \n            else\n            j--;\n        }\n\n        return a;\n    }\n};\n```
['Array', 'Two Pointers', 'C++']