1

I need to split a number into similarly sized chunks. I have two variables, which are the number and the number of desired separate chunks.

So for example if I want to split 120 in 5 chunks, a good output could be: 0-24, 25-49, 50-74, 75-99, 100-120.

The chunks don't need to be completely identical in size, it's okay if they're similar, but they need to be completely separate, i.e. a number cannot be in the two chunks.

I have checked other answers like this but they don't work because they aren't completely separate.

This is my Java code:

    int maxIndex = 10;
    int numberOfChunks= 5;
    int chunkSize =new Double( Math.floor( ((double)maxIndex) / numberOfChunks)).intValue();

    int counter=0;
    for (int i=0; i<numberOfChunks;i++) {
        int min = counter;
        int max = counter+chunkSize;
        if (i==numberOfChunks-1) max=maxIndex;
        counter+=chunkSize+1;
        System.out.println(min + " - " + max);

    }

This code does not work for small numbers because of that "+1". An example of the wrong output is:

0 - 2

3 - 5

6 - 8

9 - 11

12 - 10

Any ideas?

user3804769
  • 469
  • 1
  • 8
  • 19

3 Answers3

1

Assuming I'm reading your question correctly, and that a valid list of chunks would be {0-1, 2-3, 4-5, 6-7, 8-10}, I think you can get there using some small modifications. Something like:

int maxIndex = 10;
int numberOfChunks= 5;
int chunkSize =new Double( Math.floor( ((double)maxIndex) / numberOfChunks)).intValue();

int counter = 0;
for (int i = 0; i < numberOfChunks; i++) {
    int min = counter;
    int max = counter + chunkSize - 1;
    if (i == numberOfChunks - 1) {
        max = maxIndex;
    }
    counter += chunkSize;
    System.out.println(min + " - " + max);

}
jwismar
  • 12,164
  • 3
  • 32
  • 44
1

By using the chunkSize as you are, you are not counting the 0. This means you should subtract one when initializing the max.

int maxIndex = 10;
int numberOfChunks= 5;
int chunkSize =new Double( Math.floor( ((double)maxIndex) / numberOfChunks)).intValue();

int counter=0;
for (int i=0; i<numberOfChunks;i++) {
    int min = counter;
    int max = counter+chunkSize-1;
    if (i==numberOfChunks-1) max=maxIndex;
    counter+=chunkSize; //Get rid of the +1
    System.out.println(min + " - " + max);

}
Teun
  • 916
  • 5
  • 13
  • Without the `max = maxIndex` check, I think you end up with {0-1, 2-3, 4-5, 6-7, 8-9}, not {0-1, 2-3, 4-5, 6-7, 8-10} – jwismar Jul 18 '19 at 13:27
0

A commented solution that is explicit about using at most 2 chunk sizes that are 1 apart:

int count = maxIndex+1; // actual number of values to distribute
int size = new Double( Math.floor( ((double)maxIndex) / numberOfChunks)).intValue();  // minimum amount to put in each range
int extra = count - size*numberOfChunks;  // values left after minimums used
int i = 0; // start of current range
while (i <= maxIndex) {
    int j = i+size; // end of current range, assuming an extra is included
    if ( extra > 0 ):
        extra -= 1; // we've used one of our extras
    else:
        j -= 1;     // "remove" the extra we optimistically included
    System.out.println(i + " - " + j);
    i = j+1;  // start next range just after this one
}
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101