Take for example this input array:
For this input, I need to produce an array of total 23 elements which contains two elements of 2, seven elements of 7, one element of 1, four elements of 4 and nine elements of 9. Example of output array:
However, this array needs to be ordered in the way that minimizes the number of same elements appearing next to each other. The example of such array would be something like this:
The primary goal is to minimize the number of the same adjacent elements.
Secondary goal is to distribute elements unbiased across resulting array if possible.
Any pseudo code or C#, java, ... would be helpful.