Minimum and maximum number of N chocolates after distribution among K students
Given N Chocolates and K students, the task is to find how to divide the chocolates such that the difference between the minimum and maximum chocolate received by all students is minimized. Print the value of minimum and maximum chocolate distribution.
Input: N = 7, K = 3 Output: Min = 2, Max = 3 Distribution is 2 2 3 Input: N = 100, K = 10 Output: 10 10 Distribution is 10 10 10 10 10 10 10 10 10 10
Attention reader! All those who say programming isn't for kids, just haven't met the right mentors yet. Join the Demo Class for First Step to Coding Course, specifically designed for students of class 8 to 12.
The students will get to learn more about the world of programming in these free classes which will definitely help them in making a wise career choice in the future.
Approach: The difference will only be minimized when each student gets an equal number of candies that is N % k = 0 but if N % K != 0 then each student will 1st get (N-N%k)/k amount of candy then the rest N%k amount of candies can be distributed to N%K students by giving them each 1 candy. Thus there will be just 1 more candy than the (N-N%k)/k if N % K != 0 with a student.
Below is the implementation of the above approach: