How the math works

This version was not as tricky to implement as the off-center pie charts were. I still don’t have a polynomial-time solution to the optimization problem, and it seems similar enough to subset sum that it’s likely to be NP-hard. My first two attempts at a better-than-factorial-time algorithm were wrong. Thanks to Tomek Czajka and Mark Gordon for pointing that out.

This problem is much trickier than it looks, and a few of the obvious-sounding observations are incorrect.

Let’s set the goal of minimizing the difference in width between the widest and the narrowest slice — that will be our cost function. The first observation that jumps out is that the smallest-area slice should always be the leftmost or rightmost slice. The next obvious-looking observation is that the second smallest slice should go to the opposite side.

Proving either of these claims is tricky though, especially given that the second claim is false. The counter-example (due to Tomek) is [1, 9, 10, 80]. Try it above, and it will place the 1 and the 9 on the same side.

Implementing the full O(n!) permutation search seemed like the safer choice here, and fast enough for practical cases. So that’s what the code in this page does.