Card Sorter Queue Balance
In Card Sorter Queue Balance, the tested task is a yes-or-no parity question. You are given an array of integers and need to decide whether the number of even values and odd values is balanced enough to alternate them without one side overwhelming the other.
The practical rule is simple: if the counts of evens and odds differ by at most one, return true. Otherwise, return false. That matches the idea that you can interleave the two groups when neither side outnumbers the other by too much.
For example, cards = [1,2,1,2] returns true because there are two odd numbers and two even numbers. The counts are perfectly balanced. The array cards = [2] also returns true because one even and zero odds still differ by only one. But cards = [1,1,1] returns false because the odd side outnumbers the even side by three.
So the task is not to reorder the cards or simulate operations. It is simply to count evens and odds and check whether their counts differ by at most one.
Example Input & Output
There are two odd values and two even values, so the parity counts are perfectly balanced.
All three values are odd, so the counts differ by too much.
One even and zero odds still differ by only one, so the array counts as balanceable.
Algorithm Flow

Solution Approach
A clean way to solve this problem is to count how many values are even and how many are odd, then compare those two counts. You do not need sorting, rotation, simulation, or any special data structure. The whole problem comes down to one parity count check.
The key observation is that the array is considered balanceable when the number of even values and the number of odd values differ by at most one. If one group is much larger than the other, then you cannot alternate them in a balanced pattern. That is exactly why arrays like [1, 2, 1, 2] return true, while [1, 1, 1] returns false.
You can process the array in one pass. For each number, check num % 2. If the remainder is zero, increment the even count. Otherwise, increment the odd count. After the loop, compute the absolute difference between the two counts and see whether it is at most one.
In JavaScript, the core logic looks like this:
This directly matches every test case. An empty array returns true because both counts are zero, so the difference is zero. A one-element array such as [2] also returns true because the difference is one. Arrays made entirely of one parity, like [1, 1, 1] or [1, 3, 5], return false when that difference grows too large.
The time complexity is O(n) because you only scan the array once. The space complexity is O(1) because you only store two counters. That makes this more efficient and more natural than any approach that tries to sort or rearrange the values first.
So the full strategy is: count evens, count odds, compute the difference, and return true when the counts differ by at most one. Otherwise, return false.
Best Answers
import java.util.*;
class Solution {
public Object card_sorter_queue_balance(Object input) {
Object[] args = (Object[]) input;
int[] cards = (int[]) args[0];
String[] ops = (String[]) args[1];
LinkedList<Integer> q = new LinkedList<>();
for (int c : cards) q.add(c);
ArrayList<Integer> out = new ArrayList<>();
for (String op : ops) {
if (q.isEmpty()) break;
if (op.equals("deposit")) {
out.add(q.removeFirst());
} else if (op.equals("rotate")) {
q.add(q.removeFirst());
}
}
while (!q.isEmpty()) out.add(q.removeFirst());
int[] res = new int[out.size()];
for (int i = 0; i < out.size(); i++) res[i] = out.get(i);
return res;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
