Take for example this input array:
[2,7,1,4,9]
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:
[2,2,7,7,7,7,7,7,7,1,4,4,4,4,9,...]
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:
[7,2,9,4,9,1,7,...]
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.