Formula to evenly distribute elements without knowledge of the other buckets

814 Views Asked by At

I am trying to write a formula to determine how to evenly distribute elements into individual buckets without specific knowledge of each bucket. The only knowledge that you have is the max number of elements in a bucket, and the total number of elements in all of the buckets. I am trying to write a program which given any number of elements (not one at a time), determine how many elements get put into each bucket. The problem is that I have to access each bucket on its own. This problem would be very easy if I could iterate over the buckets wit one element at a time and put elements into the bucket with the minimum number in it currently.

For example, say I have four buckets containing marbles:

  • Bucket One - 0 marbles
  • Bucket Two - 1 marble
  • Bucket Three - 3 marbles
  • Bucket Four - 4 marbles

Say at the beginning of my program, I am given 5 marbles and need to put them in the buckets in whatever way gets the system closest to evenly distributed, which from looking at it, would mean 3 marbles go into Bucket One and 2 marbles goes into Bucket Two. Is there any formula I can run on a per bucket basis that can provide me with how many marbles should go in the bucket, with only the knowledge of how many marbles are in that current bucket, that the max number of marbles that any bucket has is 4 marbles, and that the total number of marbles in the system is 8 marbles?