Detailed Explanation Ever - Two Pointer | Most Optimised code with 100% Runtime & Memory Acceptance
Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition behind this code is to rearrange the elements in the given integer array 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind this code is to rearrange the elements in the given integer array nums in such a way that all even integers are placed at the beginning of the array, followed by all the odd integers.\n\n---\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialize two pointers, left and right, with left pointing to the beginning of the array and right pointing to the end of the array.\n\n2. Use a while loop to iterate through the array while left is less than right.\n\n3. Inside the loop, check if the number at the left pointer is even (i.e., nums[left] % 2 == 0). If it\'s even, increment the left pointer to move to the next element.\n\n4. If the number at the left pointer is odd, swap it with the number at the right pointer and decrement the right pointer. This effectively moves the odd number to the end of the array.\n\n5. Continue this process until left is no longer less than right, ensuring that all even integers are placed at the beginning and odd integers at the end of the array.\n\n6. Finally, return the modified nums array.\n\n---\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe code iterates through the input array once using the two-pointer approach. In the worst case, it needs to traverse the entire array once. Therefore, the time complexity is O(n), where n is the number of elements in the input array nums.\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe code uses a constant amount of extra space for the two pointers (left and right). It doesn\'t use any additional data structures or allocate memory proportional to the input size. Therefore, the space complexity is O(1), which represents constant space usage.\n\n---\n# Do Upvote if you liked the explanation \uD83E\uDD1E\n\n---\n\n\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int> sortArrayByParity(vector<int>& nums) {\n        int left = 0; // Pointer for even numbers\n        int right = nums.size() - 1; // Pointer for odd numbers\n        \n        while (left < right) {\n            if (nums[left] % 2 == 0) {\n                // If the number at the left pointer is even, move the left pointer to the right\n                left++;\n            } else {\n                // If the number at the left pointer is odd, swap it with the number at the right pointer\n                swap(nums[left], nums[right]);\n                right--;\n            }\n        }\n        \n        return nums;\n    }\n};\n\n```
['Array', 'Two Pointers', 'C', 'Sorting', 'Heap (Priority Queue)', 'Merge Sort', 'Python', 'C++', 'Java', 'Python3']