I have the following problem: I have M numbers arranged into a line. I need to divide the line into N groups such that the sums of the numbers of each group are closest to the mean of these sums by some metric. The actual metric is not important: we can choose to minimize sum of absolute differences, or variance, etc., depending on which leads to the simplest solution.
A similar problem is partitioning of sets, which is NP Hard. However, here we have additional constraint: groups must pack successive numbers, so there might be a solution that doesn’t involve brute-force search. The numbers are large.
EDIT
Example:
Numbers: 1 2 3 4 5 6 7 8 9 10, need to divide into 3 groups
Let’s say we want to minimize sum of absolute differences (SAD).
Groups: (1) 1 2 3 4 5 6 (sum = 21); (2) 7 8 (sum = 15); (3) 9 10 (sum = 19)
Mean = (21+15+19)/3 = 18.33, SAD = 21-18.33 + 18.33-15 + 19-18.33 = 6.67 <- That’s what we want to minimize.
Once you know what the sum should be, then you can make groups that are close to this sum. If your metrics are nice then you should be able to use binary search to find what the actual sum is. When you are aiming for a particular sum you could go through the list adding numbers to a group until the groups sum goes over the sum size. Then either take or don’t take this last integer. go through the entire list doing this and see what groups sum deviated the most from the sum. Then go back through the list trying combinations of group sizes that fall within the deviation.. it should be fast enough. Otherwise use dynamic programming.