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.
Algorithm to order array
java;c#;algorithms
This can be seen as a traveling salesman problem:each element is a node of your graph, labled with it's numberthere are edges between each pair of nodesthe distance of getting from one node to another is 0 if the lables have different numbers, and 1 if the numbers are equalNow you are looking for a route through this graph minimizing the total distance. Google for                traveling salesman <your_favorite_programming_language>and you will find tons of example implementations with heuristic or brute-force approaches.