Java Array Program · A08
Separate Even and Odd Numbers in an Array in Java
Split an array into evens and odds in one pass. The detail that catches people out is how Java's % operator treats negative numbers.
Problem Statement
Write a method that splits the elements of an int array into two groups: even numbers and odd numbers. Each group keeps the original order.
Zero is even, and negative numbers must be classified correctly. Either group can be empty.
Input/Output Examples
[7, 2, 9, 4, 6, 1]OutputEven=[2, 4, 6], Odd=[7, 9, 1]Explanation: each number goes into its group in the order it appears.[-3, -2, 0]OutputEven=[-2, 0], Odd=[-3]Explanation: -2 and 0 are even; -3 is odd.[5]OutputEven=[], Odd=[5]Explanation: the even group is empty.Visual Explanation
[7, 2, 9, 4, 6, 1]: even numbers (purple) and odd numbers (amber) are appended to their own group in reading order.Each number is tested once with n % 2 == 0. Because elements are appended in the order they are read, both groups keep the relative order of the input.
Approaches
Both approaches keep the original order inside each group. The loop needs one pass; the stream version uses two filters.
Approach 1: Single Loop With Two Lists (Recommended)
Idea: read the array once and add each number to an even or odd list.
- Create two empty
ArrayLists. - For each number, test
n % 2 == 0. - Add it to
evenif the test passes, otherwise toodd.
Time: O(n) · Space: O(n).
Approach 2: Two Stream Filters
Idea: filter the array twice: once for even numbers and once for odd numbers.
- Collect
filter(n -> n % 2 == 0)into anint[]. - Collect
filter(n -> n % 2 != 0)into anotherint[]. - Print or return both arrays.
Time: O(n) · Space: O(n). It reads the array twice.
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Single Loop With Two Lists (Recommended)
Lists grow as needed, so you do not have to count the evens before creating the result.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class EvenOddLists {
// n % 2 == 0 is true for negative even numbers too; n % 2 == 1 would miss -3
static void separate(int[] arr) {
List<Integer> even = new ArrayList<>();
List<Integer> odd = new ArrayList<>();
for (int n : arr) {
if (n % 2 == 0) {
even.add(n);
} else {
odd.add(n);
}
}
System.out.println(Arrays.toString(arr) + " -> Even=" + even + ", Odd=" + odd);
}
public static void main(String[] args) {
int[][] inputs = {{7, 2, 9, 4, 6, 1}, {-3, -2, 0}, {5}};
for (int[] arr : inputs) {
separate(arr);
}
}
}
[7, 2, 9, 4, 6, 1] -> Even=[2, 4, 6], Odd=[7, 9, 1] [-3, -2, 0] -> Even=[-2, 0], Odd=[-3] [5] -> Even=[], Odd=[5]
Time complexity: O(n) · Space complexity: O(n).
Approach 2: Two Stream Filters
Short and declarative, and the result stays as primitive int[] arrays without boxing.
import java.util.Arrays;
public class EvenOddStream {
// Two filter passes; each keeps the original order of its group
static void separate(int[] arr) {
int[] even = Arrays.stream(arr).filter(n -> n % 2 == 0).toArray();
int[] odd = Arrays.stream(arr).filter(n -> n % 2 != 0).toArray();
System.out.println(Arrays.toString(arr) + " -> Even=" + Arrays.toString(even)
+ ", Odd=" + Arrays.toString(odd));
}
public static void main(String[] args) {
int[][] inputs = {{7, 2, 9, 4, 6, 1}, {-3, -2, 0}, {5}};
for (int[] arr : inputs) {
separate(arr);
}
}
}
[7, 2, 9, 4, 6, 1] -> Even=[2, 4, 6], Odd=[7, 9, 1] [-3, -2, 0] -> Even=[-2, 0], Odd=[-3] [5] -> Even=[], Odd=[5]
Time complexity: O(n) · Space complexity: O(n).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Single Loop With Two Lists | O(n) | O(n) |
| Two Stream Filters | O(n) | O(n) |
Here n is the number of elements. Every element is tested once (twice for the stream version), and the two groups together hold all n elements.
Interview Tips and Recommended Approach
- Test even, not odd. In Java,
-3 % 2is-1, son % 2 == 1misses negative odd numbers; usen % 2 != 0orn % 2 == 0. - Mention the bitwise test.
(n & 1) == 0is also correct for negatives and avoids the division. - Ask whether order matters. If it does not, a two-pointer partition can move evens to the front of the same array in
O(1)extra space. - Know the collectors option:
Collectors.partitioningBy()splits aList<Integer>into aMap<Boolean, List<Integer>>in one call.
Recommended Approach: Single Loop With Two Lists
Why: it needs only one pass, keeps the original order of both groups and handles negative numbers by testing for even. Each step is plain and easy to explain.