Problem of "clustering" into most similar groups I do not think that the following problem can be solved with k-means clustering. I am not sure though. Okay, let me describe the problem. I need to find a way or an algorithm that groups members of a given data set (of positive integers) so that the difference between group means is minimized (not maximized, as usual). There are two constraints:


*

*The number of groups should not exceed log(N) base 2. N is the input array size. Let us assume that N = 16, always.

*The size of the group should be at least log(N) base 2. Here it is 4.
Please see below example. Guidance toward a solution to this problem is appreciated.
   for input array = (12, 14, 16, 16, 18, 19, 20, 21, 24, 26, 27, 29, 29, 30, 31, 32)

One of the solutions could be the following:
set    group           mean
1   (12, 14, 31, 32)    22.25
2   (16, 16, 29, 30)    22.75
3   (18, 19, 27, 29)    23.25
4   (20, 21, 24, 26)    22.75

 A: This is not really clustering. It reminds me of the classic bin packing problem, which unfortunately is NP-hard.
https://en.wikipedia.org/wiki/Bin_packing_problem
Seriously, avoid the term "cluster". Cluster usually refers to "similar" objects, here they are very dissimilar.
A classic strategy would be to first compute the optimal mean (= mean of all data). Then use a greedy strategy to build groups reasonably close to this mean, e.g. by combining high and low values. Then finally, try to optimize the result by moving or swapping objects to improve the target criterion until convergence. You could even try a genetic algorithm for this.
A: Here's one not tested idea how to adapt K-means algo to try to solve your task. You may know that the majority of standard K-means clustering implementations include the so called running means option. Under this option, on each iteration the cluster centre is being updated (recalculated) immediately after a point is assigned to that cluster (rather than after all points were assigned to their closest clusters, as in typical mode). If you use running means in conjunction with such modification of the algo that a point is assigned to the farthest (rather than the nearest) cluster, the centre, then cluster centres are expected to converge during the whole process, and you end with clusters which are maximally superposed.
