Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>n/5</code> will give you the number of buckets of size 5 that will be full.</p> <p><code>n%5</code> will give you the rest that couln't be placed.</p> <p>If <code>n%5 &gt;= 2</code>, create a new bucket and place those.</p> <p>If <code>n%5 = 1</code>, create a new bucket, add it, and pick one in another bucket.</p> <p>This example demonstrates the algorithm :</p> <pre><code>public class Test { public static void main(String[] args) { place(21,5); } public static void place(int number, int sizeBucket){ int nbBuckets = number / sizeBucket; int nbLeft = number % sizeBucket; List&lt;Bucket&gt; lBuckets = new ArrayList&lt;&gt;(); for(int i = 0; i &lt; nbBuckets; i++){ lBuckets.add(new Bucket(sizeBucket, sizeBucket)); } if(nbLeft &gt;= 2) lBuckets.add(new Bucket(5, nbLeft)); else if (nbLeft == 1){ Bucket b = lBuckets.get(lBuckets.size()-1); b.setSize(b.getSize()-1); lBuckets.add(new Bucket(sizeBucket, nbLeft+1)); } System.out.println(lBuckets); } } class Bucket { private int capacity; private int size; public Bucket(int capacity, int size) { super(); this.capacity = capacity; this.size = size; } public int getCapacity() { return capacity; } public void setCapacity(int capacity) { this.capacity = capacity; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } @Override public String toString() { return "Bucket [capacity=" + capacity + ", size=" + size + "]"; } } </code></pre> <p>Output :</p> <pre><code>[Bucket [capacity=5, size=5], Bucket [capacity=5, size=5], Bucket [capacity=5, size=5], Bucket [capacity=5, size=4], Bucket [capacity=5, size=2]] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload