878871. Even Odd Game

Easy

Even Odd Game

Slug: even-odd-game--153136 Difficulty: Easy Id: 878871 Topic Tags: CPP, Java, C

Summary

The Even Odd Game is a simple algorithmic problem that involves manipulating numbers based on their parity (even or odd). The goal is to separate the input array into two arrays: one for even numbers and another for odd numbers. This problem requires basic understanding of conditional statements and array manipulation.

Detailed Explanation

To solve this problem, we will iterate through the input array and check each number's parity using a simple modulo operation (n % 2 == 0). If the remainder is 0, the number is even; otherwise, it's odd. We will then append each number to either an even or odd array based on its parity.

Here's a step-by-step breakdown of the solution:

  1. Initialize two arrays, even and odd, to store the processed numbers.
  2. Iterate through the input array using a for loop or similar construct.
  3. For each number in the input array:
    • Check if the number is even by performing the modulo operation (n % 2 == 0).
    • If true, append the number to the even array; otherwise, append it to the odd array.
  4. Return the even and odd arrays as the final result.

Time complexity: O(n), where n is the size of the input array, since we only iterate through the array once. Space complexity: O(n), as we create two separate arrays to store the processed numbers.

Optimized Solutions

Java

View on GeeksforGeeks
public class EvenOddGame {
    public static int[][] evenOdd(int[] arr) {
        int[][] result = new int[][]{{}, {}};
        for (int num : arr) {
            if (num % 2 == 0) {
                result[0].add(num);
            } else {
                result[1].add(num);
            }
        }
        return result;
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.