Untitled

mail@pastecode.io avatar
unknown
java
7 months ago
1.0 kB
2
Indexable
Never
public class Solution {
    public static void main(String[] args) {
        // Example input
        int[] nums = {10, 98, 3, 33, 12, 22, 21, 11};

        // Processing the array
        int[] result = reorderEvenOdd(nums);

        // Output
        for (int n : result) {
            System.out.print(n + " ");
        }
    }

    private static int[] reorderEvenOdd(int[] nums) {
        int evenCount = 0;

        // First pass: count the number of even numbers
        for (int num : nums) {
            if (num % 2 == 0) {
                evenCount++;
            }
        }

        int[] result = new int[nums.length];
        int evenIndex = 0;
        int oddIndex = evenCount;

        // Second pass: fill the result array
        for (int num : nums) {
            if (num % 2 == 0) {
                result[evenIndex++] = num;
            } else {
                result[oddIndex++] = num;
            }
        }

        return result;
    }
}
Leave a Comment