Video Solution | Explanation with drawings | In Depth | C++ | Java
Intuition and approach discussed in detail in video solution\nhttps://youtu.be/JEW63L8X94E\n# Code\nC++\n\nclass Solution {\npublic:\n    vector<int> sortArrayB
# Intuition and approach discussed in detail in video solution\nhttps://youtu.be/JEW63L8X94E\n# Code\nC++\n```\nclass Solution {\npublic:\n    vector<int> sortArrayByParity(vector<int>& nums) {\n        int sz = nums.size(), left = 0, right = sz-1;\n        while(left < right){\n            \n            while(left < right && nums[left] % 2 == 0){\n                left++;\n            }\n            while(right > left && nums[right] % 2 != 0){\n                right--;\n            }\n            if(left < right){\n                swap(nums[left], nums[right]);\n            }\n        }\n        return nums;\n    }\n};\n```\nJava\n```\nclass Solution {\n    public int[] sortArrayByParity(int[] nums) {\n        int sz = nums.length, left = 0, right = sz-1;\n        while(left < right){\n            while(left < right && nums[left] % 2 == 0){\n                left++;\n            }\n            while(right > left && nums[right] % 2 != 0){\n                right--;\n            }\n            if(left < right){\n                swap(nums, left, right);\n            }\n        }\n        return nums;\n    }\n    private void swap(int nums[], int left, int right){\n        int temp = nums[left];\n        nums[left] = nums[right];\n        nums[right] = temp;\n    }\n}\n```
['C++']