Candies Division

Someone plz explain me the logic behind solution of this problem

First of all you can only distribute either n/k candies or n/k+1(if(n%k!=0).
Secondly the amount of n/k+1 candies should be less than or equal to k/2.
that’s it. Now comes the implementation.
if n%k==0 simply distribute n/k candies to all and n is the answer.
otherwise first distribute n/k candies to all and then the remaining candies will be n%k. if n%k <=k/2 (2nd condition) then n will be the answer but this condition fails the answer will be (n/k)*k+k/2.

1 Like