Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dividing a list of values into three equal subtotals

I have a list of numbers which total 540000. I would like to sort this list into 3 lists which each total 180000. What is the most efficient programming method to do this, assuming that the list of numbers is a flat file with a number per line?

like image 907
Andy Avatar asked Nov 22 '25 14:11

Andy


1 Answers

Sounds like a variation of the Knapsack problem . It would be useful to know the size of these numbers, and count - are there huge variations in size, or are they all similar in scale - are there lots of them (>1000) or just a few (<100)?

One quick and dirty method would be to sort them into size order - largest to smallest - then loop over them, putting the first in the first list, the second into the second list, the third into the third list, and then go back and put the fourth into the first list... and so on. May work quite well for lots of small-ish numbers... but there are other approaches for different types fo dataset.

like image 95
Dycey Avatar answered Nov 24 '25 04:11

Dycey