BudiBadu Logo

Card Sorter Queue Balance

Sorting Easy 3 views
Like21

The library’s registration desk uses a two-sided card sorter for membership applications. In Card Sorter Queue Balance, you’re given a series of card IDs. Your task is to divide them into two queues: even-numbered IDs and odd-numbered IDs. This keeps the intake flow balanced for the staff!

The "secret sauce" is Parity Partitioning. You iterate through the input and use the modulo operator (% 2) to determine if a card belongs in the even or odd group. Crucially, each queue must maintain its original "arrival order" from the source. Returning these two lists as a pair providing a clean split for the desk team. This simple routine helps prevent bottlenecks and keeps the library's rush moving at a steady pace. It’s a great way to practice data separation and index management in one go!

Because this is a counting/optimization-style challenge, dynamic programming is usually the safest approach: define what each index or state means, initialize valid base states, and make transitions explicit. If the problem uses modulo arithmetic, apply modulo at every accumulation step so large intermediate totals never corrupt the final answer.

Examples

Example 1
Input
cards = [5,2,9,7], operations = ["deposit","rotate","deposit","deposit"]
Output
[5,9,7,2]
Explanation

The machine deposits 5, rotates to send 2 to the back, deposits 9, then deposits 7; the remaining queue holds only 2.

Example 2
Input
cards = [4,1,3], operations = ["rotate","deposit","deposit"]
Output
[1,3,4]
Explanation

After rotating, the queue becomes [1,3,4]; depositing twice produces [1,3], leaving [4] in the queue.

Example 3
Input
cards = [8,6], operations = ["deposit","deposit"]
Output
[8,6]
Explanation

Both cards move directly to the output pile.

Algorithm Flow

Recommendation Algorithm Flow for Card Sorter Queue Balance - Budibadu
Recommendation Algorithm Flow for Card Sorter Queue Balance - Budibadu

Best Answers

java
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;
    }
}