Divide items with integer ID-s into N equal groups, based on ID-s

60 Views Asked by At

I have unknown number of items, each having ID (consecutive integer numbers), ie.

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15...

I want to split above items into as equal size N groups as possible, using algorithm based on the ID.

Important! The input data is only:

  • X (group size)
  • ID of current item

The algorithm is simple when the group size is 2:

if ID mod 2 == 0 then
    group 1
else
    group 2

But what if group size is ie. 7?

1

There are 1 best solutions below

0
On

if ID mod N == $0$ then

group $1$

else if ID mod N == 1 then

group $2$

$\ddots$

else if ID mod N == $5$ then

group $6$

else

group $7$