So i’m going to spitball ideas. There’s *probably* a mathematical formula for this, but heres how it plays out in my head, but I already know my plan has a couple of flaws.

Well, your target is the average. You will need to keep track of the number of tables remaining.

If there exists one or more values that equal this average, remove them from your array; they go into a table each.

Now it gets more difficult.

Phase 2:

Find the total of all combinations of 2 values. If any of them equal the average, remove them from the array, and put those numbers into a table.

Eventually, there will be either no more sets of two-value pairings, or no more values in the array.

Repeat this process with 3 (if 3 or more elements remain), then 4, etc… until the selection set is of size TotalLength-X, where X is the number of remaining tables to fill.

Phase 3: (AKA the ‘it didnt work’ phase)

Repeat the process above, but store the results of each combination in a multidimensional array, keyed by the absolute value of the difference between the average and total (the *distance from the average*), and holding an array of combinations.

Key Sort the array.

Take an element from the first element of the array; assign it to a table.

Remove from that first key any other element that uses a component of the one you selected.

If the first key is not empty and there are more tables to be filled, remove another element from the key’s array and repeat.

Repeat phase 3 until the tables are all filled.

So what’s the flaw?

The flaw is that this algorithm will get as close as it can to the average at each step - but that doesn’t mean it gets the best values for *all* the steps as a whole. You might end up with a set that generates 100-95-94-91, or a set that generates 100-99-98-20.

What you *could* do is add a phase 4, which combines unique combinations of sets from the Phase 3 table, and finds sets that are closest to X*Mean, where X is the number of remaining tables.

Also keep in mind that this is designed for **small** sets of numbers. The number of combinations of sets grows exponentially-stupid (EDIT: Actually factorially-stupid.) the more numbers exist in your set.