EASY SOLUTION IN JAVA BEATING 99.94% IN 2ms
IntuitionWe are given an array nums where half of the integers are even and the other half are odd. Our goal is to rearrange the array such that:
- Even integer
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
We are given an array nums where half of the integers are even and the other half are odd. Our goal is to rearrange the array such that:
    - Even integers are placed at even indices (i.e., indices 0, 2, 4, …).
    - Odd integers are placed at odd indices (i.e., indices 1, 3, 5, …).
# Approach
<!-- Describe your approach to solving the problem. -->
1. **Two Pointer Technique:**
    - **We use two indices:** evenIndex to track the next available even index (starting from 0) and oddIndex to track the next available odd index (starting from 1).
    - **Iterate through the array:**
        - For each number in nums, check if it is even or odd.
        - If the number is even, place it at the current evenIndex and then increment evenIndex by 2 to move to the next available even position.
        - If the number is odd, place it at the current oddIndex and then increment oddIndex by 2 to move to the next available odd position.
2. **Fill the Result Array:**
    - During the iteration, we populate the result array with the appropriate numbers in their respective indices.
3. **Final Output:** 
    - After processing all numbers, the result array will contain the even numbers at even indices and odd numbers at odd indices, as required.
# Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(n)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

![image.png](https://assets.leetcode.com/users/images/70fa4bf9-fadb-4c6c-bb20-3cd93d6d8906_1738563381.3005545.png)
# Code
```java []
class Solution {
    public int[] sortArrayByParityII(int[] nums) {
        int n = nums.length;
        int[] result = new int[n];
        
        int evenIndex = 0;
        int oddIndex = 1;
        
        for (int num : nums) {
            if (num % 2 == 0) {
                result[evenIndex] = num;
                evenIndex += 2;
            } else {
                result[oddIndex] = num;
                oddIndex += 2;
            }
        }
        
        return result;
    }
}

```
['Array', 'Two Pointers', 'Sorting', 'Java']