Skip to content
Advertisement

How to find maximum group size dynamically

I want to find the maximum group size g_new if I want to partition a list of ‘n’ values. We can have any number of groups.

I have: n values and maximum group size possible g_max.

e.g.

n = 110 and g_max = 25.

We cannot form groups of size: [28,28,27,27] as no group should be more than 25!

Here, Solution is, g_new can be 22. as it will form 5 groups of 22 each.

My question is:

How can I find dynamically, this g_new (22 here) value, given : n and g_max

Note that I want to form biggest possible groups without violating g_max!

Sorry, if this question is too simple, but I cannot get my head around it.

Advertisement

Answer

Assuming you meant n=110 and want group sizes to differ by at most 1, you could first compute the up-rounded number of needed groups and then the up-rounded group size:

>>> n, g_max = 110, 25
>>> -(n // (-n // g_max))
22
>>> n, g_max = 110, 36
>>> -(n // (-n // g_max))
28
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement