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
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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Number of chocolates left after k iterations
- Maximum students to pass after giving bonus to everybody and not exceeding 100 marks
- Number of subarrays whose minimum and maximum are same
- Number of ways to distribute N Paper Set among M students
- Minimum number of elements to be removed to make XOR maximum
- Sum of Maximum and Minimum prime factor of every number in the Array
- Minimum and Maximum element of an array which is divisible by a given number k
- Minimum and Maximum number of pairs in m teams of n people
- Break a number such that sum of maximum divisors of all parts is minimum
- Number of times Maximum and minimum value updated during traversal of array
- Distribution of a Number in Array within a Range
- Random number generator in arbitrary probability distribution fashion
- Count the number of ways to give ranks for N students such that same ranks are possible
- Puzzle | Selling Chocolates
- Minimum distance between the maximum and minimum element of a given Array
- Maximum and Minimum value of a quadratic function
- Minimum integer with at most K bits set such that their bitwise AND with N is maximum
- Find the maximum possible value of the minimum value of modified array
- Minimum and maximum possible length of the third side of a triangle
- Sum of all differences between Maximum and Minimum of increasing Subarrays
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.