Simple and Easy Approach C++ Code!💯✅☮️
\n\n# Code\ncpp []\nclass Solution {\npublic:\n    vector<int> sortArrayByParity(vector<int>& nums) {\n        int l = 0;\n        int r = 0;\n\n        while (
\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    vector<int> sortArrayByParity(vector<int>& nums) {\n        int l = 0;\n        int r = 0;\n\n        while (r < nums.size()) {\n            if (nums[r] % 2 == 0) {\n                swap(nums[l], nums[r]);\n                l++;\n            }\n            r++;\n        }\n\n        return nums;\n    }\n};\n```\n\nHere\'s a detailed explanation of the code to sort an array by parity, including intuition, approach, example walkthrough, and complexity analysis:\n\n### Intuition\n\nThe goal is to rearrange an array so that all even numbers appear before all odd numbers while preserving their relative order. This can be achieved efficiently by using a two-pointer technique where one pointer tracks the position for even numbers (`l`) and the other pointer (`r`) scans through the array.\n\n### Approach\n\n1. **Initialization:**\n   - Initialize two pointers: `l` to track the position where the next even number should be placed, and `r` to traverse the array.\n\n2. **Traversal:**\n   - Use `r` to iterate through the array from the start to the end.\n   - For each element, check if it is even.\n   - If it is even, swap it with the element at index `l`, and then increment `l` (to update the position for the next even number).\n\n3. **Increment `r`:**\n   - Move `r` to the next position, regardless of whether the current element is even or odd.\n\n4. **Termination:**\n   - Continue this process until `r` has traversed the entire array.\n\n### Example Walkthrough\n\nLet\'s walk through an example to see how the code works:\n\n**Example: `nums = [3, 1, 2, 4]`**\n\n1. **Initial State:**\n   - `nums = [3, 1, 2, 4]`\n   - `l = 0`, `r = 0`\n\n2. **First Iteration (`r = 0`):**\n   - `nums[0] = 3` (odd)\n   - No swap. Increment `r` to `1`.\n\n3. **Second Iteration (`r = 1`):**\n   - `nums[1] = 1` (odd)\n   - No swap. Increment `r` to `2`.\n\n4. **Third Iteration (`r = 2`):**\n   - `nums[2] = 2` (even)\n   - Swap `nums[l]` (3) with `nums[r]` (2). Now `nums = [2, 1, 3, 4]`.\n   - Increment `l` to `1` and `r` to `3`.\n\n5. **Fourth Iteration (`r = 3`):**\n   - `nums[3] = 4` (even)\n   - Swap `nums[l]` (1) with `nums[r]` (4). Now `nums = [2, 4, 3, 1]`.\n   - Increment `l` to `2` and `r` to `4`.\n\n6. **Termination:**\n   - `r` is now out of bounds. The final array is `[2, 4, 3, 1]`, with all even numbers at the beginning and odd numbers at the end.\n\n### Complexity Analysis\n\n- **Time Complexity:** `O(n)`\n  - The `while` loop iterates through the array once, where `n` is the number of elements in the array. Each element is processed a constant number of times (specifically, once during the swap operation).\n\n- **Space Complexity:** `O(1)`\n  - The algorithm uses a constant amount of extra space, as it only involves a few variables for pointers and no additional data structures are used.\n\n### Summary\n\nThis approach efficiently segregates even and odd numbers using a single pass through the array and constant extra space. The key operation is the swap, which ensures that even numbers are moved to the front while odd numbers stay at the end.
['C++']