JAVA | Beats 100% Time | O(N) Time O(1) Space | Two Pointers | Visual Example | Detailed Explanation
\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \nThe goal is to rearrange the elements of the given array so that all even integer
![Screenshot 2023-09-28 080212.png](https://assets.leetcode.com/users/images/59e5a6c3-d201-4c12-95d2-1a9c8d57423e_1695870844.3169916.png)\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe goal is to rearrange the elements of the given array so that all even integers appear first, followed by all odd integers. We can achieve this by using a two-pointer approach. The left pointer starts at the beginning of the array, and the right pointer scans the array from left to right. Whenever the right pointer encounters an even integer, it swaps places with the element pointed to by the left pointer, effectively moving it to the left side of the array. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. We start by defining two pointers, `left` and `right`, both initially pointing to the start of the array. We also store the length of the input array in the variable `n`.\n\n2. We use a `while` loop to iterate through the array with the right pointer. This loop continues until the right pointer reaches the end of the array `(right < n)`.\n\n3. Inside the loop, we check if the element at `nums[right]` is an even number. We do this by checking if `nums[right] % 2 == 0`. If it\'s even, we proceed with the following steps:\n\n    -  We swap the elements at `nums[left]` and `nums[right]`. This swap effectively moves the even integer (found at `nums[right]`) to the `left` side of the array.\n\n    - We increment the `left` pointer by one, as we\'ve placed an even integer at the correct position.\n\n4. Regardless of whether the element at `nums[right]` is even or odd, we always increment the `right` pointer by one, moving forward in the array.\n\n5. We repeat this process until the `right` pointer reaches the end of the array.\n\n6. Finally, we return the modified `nums` array. Now, all the even integers are positioned at the beginning of the array, and their relative order is preserved.\n\n# Visual Example\n![WhatsApp Image 2023-09-28 at 08.22.05.jpeg](https://assets.leetcode.com/users/images/cecded4e-040b-4238-bdfd-66849dd6d2db_1695871409.4043052.jpeg)\n\n# Complexity\n- Time complexity: `O(n)`, where `n` is the length of the input array nums. We traverse the array once.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: `O(1)`, as we use only a constant amount of extra space.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\nFor a visual demonstration and detailed explanation, watch this YouTube video:\n\n[https://youtu.be/6u21IgnnepY]()\n\n# Code\n```\nclass Solution {\n    public int[] sortArrayByParity(int[] nums) {\n        int left = 0, right = 0;\n        int n = nums.length;\n        \n        while (right < n) {\n            if (nums[right] % 2 == 0) {\n                int temp = nums[left];\n                nums[left] = nums[right];\n                nums[right] = temp;\n                left++;\n            }\n            right++;\n        }\n        return nums;\n    }\n}\n```\nPlease upvote if you like the solution.
['Two Pointers', 'Java']