Detailed Explanation Ever - Two Pointer | Most Optimised code - Beats 100% Acceptance Rate
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, while preserving the relative order of even and odd numbers.\n\n---\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialize a pointer left to keep track of the position where even numbers should be placed in the array.\n\n2. Iterate through the array using a for loop with a pointer right that moves from left to right.\n\n3. For each element at the right pointer, check if it\'s even (i.e., nums[right] % 2 == 0).\n\n4. If the number at the right pointer is even, swap it with the number at the left pointer and increment the left pointer. This effectively moves the even number to the correct position at the beginning of the array.\n\n5. Continue this process until the right pointer has traversed the entire 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 for loop. 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 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\n# Do Upvote if you liked the explanation \uD83E\uDD1E\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\n        for (int right = 0; right < nums.size(); ++right) {\n            if (nums[right] % 2 == 0) {\n                // If the number at the right pointer is even, swap it with the number at the left pointer\n                swap(nums[left], nums[right]);\n                left++;\n            }\n        }\n\n        return nums;\n    }\n};\n\n```
['Array', 'Math', 'Two Pointers', 'C', 'Sorting', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']