Above my head, create a queue containing all the blocks that you want to insert in decreasing amounts (i.e. using the example above, the queue will have 12 reds, then 8 blues, then 5 yellow, and then 1 green). Insert an element from the queue into each even index of the array, and then each odd index (i.e., insert the red block with index 0,2,4,6,8,10,12,14,16,18,20,22, then insert the blues in 24,1,3,5,7,9,9,11,13, then insert the yellow spots on 15,17, 19,21 and insert the green color in 23)
Please note that for some combinations of blocks this task is impossible - before starting the algorithm, you should check that the set of blocks with the largest number does not have more blocks than the sum of all blocks divided by 2
source share