Best Solution in C++ for Arrays 🚀 || 100% working
Intuition 🤔We need to separate even and odd numbers in the array. Let's use two pointers: one starting from the beginning (i) and the other from the end (j). 🎯A
# Intuition 🤔
We need to separate even and odd numbers in the array. Let's use two pointers: one starting from the beginning (`i`) and the other from the end (`j`). 🎯

# Approach 🚀
1. Use a **two-pointer approach**.
2. Move `i` forward if it's pointing to an even number.
3. Move `j` backward if it's pointing to an odd number.
4. If `nums[i]` is odd and `nums[j]` is even, swap them! 🔄
5. Repeat until `i` and `j` meet.

# Complexity 📊
- **Time complexity:** $$O(n)$$ since we traverse the array once.
- **Space complexity:** $$O(1)$$ as we sort in place.

# Code 💻
```cpp
class Solution {
public:
    vector<int> sortArrayByParity(vector<int>& nums) {
        int n = nums.size();
        int i = 0, j = n - 1;
        while (j > i) {
            if (nums[i] % 2 != 0 && nums[j] % 2 == 0) {
                swap(nums[i], nums[j]);
            }
            while (i < j && nums[i] % 2 == 0) i++;
            while (i < j && nums[j] % 2 != 0) j--;
        }
        return nums;
    }
};

```
<img src="https://assets.leetcode.com/users/images/7b864aef-f8a2-4d0b-a376-37cdcc64e38c_1735298989.3319144.jpeg" alt="upvote" width="150px">

# Connect with me on LinkedIn for more insights! 🌟 Link in bio
['C++']