Split Array into Queues

Split an array into 3 queues

Split Array into Three Queues

Problem Statement

Given a fixed array of integers, write constraints to split every element of the array into three separate queues — queue0, queue1, and queue2 — such that each element from the original array ends up in exactly one queue, and the three queues are evenly balanced.

Your task is to write constraints on a randomized "bin selector" array that decides which queue each element of the given array is routed into: 1. Every element of the given array must be routed to exactly one of the three queues 2. Each queue must end up with the same number of elements 3. No element may be dropped, and no element may be duplicated across queues

Constraints

  • Given array size: 9 elements (fixed): [12, 45, 7, 23, 89, 3, 56, 34, 19]
  • Number of queues: 3
  • Each queue must receive exactly ARRAY_SIZE / 3 = 3 elements
  • bin_id[i] selects which queue given_array[i] is placed into: bin_id[i] ∈ {0, 1, 2}

Examples

Example 1:

Given array: [12, 45, 7, 23, 89, 3, 56, 34, 19]
bin_id:      [ 0,  1, 2,  0,  1, 2,  0,  1,  2]

queue0: [12, 23, 56]
queue1: [45, 89, 34]
queue2: [7, 3, 19]

Explanation: Each queue gets exactly 3 elements, and every element from the given array appears exactly once across the three queues.

Example 2:

Given array: [12, 45, 7, 23, 89, 3, 56, 34, 19]
bin_id:      [ 0,  0, 0,  1,  1, 1,  2,  2,  2]

queue0: [12, 45, 7]
queue1: [23, 89, 3]
queue2: [56, 34, 19]

Explanation: A different valid split. The order of bin assignment doesn't matter — only that every queue ends up with 3 elements and every array element is accounted for exactly once.

Example 3 (Invalid):

bin_id: [0, 0, 0, 0, 1, 1, 2, 2, 2]

queue0: [12, 45, 7, 23]  (4 elements)
queue1: [89, 3]          (2 elements)
queue2: [56, 34, 19]     (3 elements)

Explanation: queue0 has 4 elements while queue1 has only 2 — the split is not balanced.

Example 4 (Invalid):

bin_id: [0, 1, 2, 0, 1, 2, 0, 1, 1]

queue0: [12, 23, 56]
queue1: [45, 89, 34, 19]
queue2: [7, 3]

Explanation: queue1 has 4 elements while queue2 has only 2 — the split is not balanced, even though every element still appears exactly once.

Loading the interactive workspace...