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.

**Examples**:

Input: N = 7, K = 3Output: Min = 2, Max = 3 Distribution is 2 2 3Input: N = 100, K = 10Output: 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:

## CPP

`// CPP implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Driver code ` `int` `main(){ ` ` ` ` ` `int` `n = 7, k = 3; ` ` ` ` ` `if` `(n % k == 0) ` ` ` `cout<<n/k<<` `" "` `<<n/k; ` ` ` `else` ` ` `cout<<((n-(n % k))/k)<<` `" "` ` ` `<<(((n-(n % k))/k) + 1); ` ` ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by Sanjit_Prasad ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` ` ` `public` `class` `Improve { ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `7` `; ` ` ` `int` `k = ` `3` `; ` ` ` ` ` `if` `(n % k == ` `0` `) ` ` ` `System.out.println(n / k +` `" "` `+ n / k); ` ` ` ` ` `else` ` ` `System.out.println((n-(n % k)) / k + ` `" "` ` ` `+ (((n-(n % k))/ k) + ` `1` `) ) ; ` ` ` ` ` `} ` ` ` `// This Code is contributed by ANKITRAI1 ` `} ` |

*chevron_right*

*filter_none*

## Python

`# Python implementation of the above approach ` ` ` `n, k ` `=` `7` `, ` `3` `if` `(n ` `%` `k ` `=` `=` `0` `): ` ` ` `print` `(n` `/` `/` `k, n` `/` `/` `k) ` ` ` `else` `: ` ` ` `print` `((n` `-` `n ` `%` `k)` `/` `/` `k, (n` `-` `n ` `%` `k)` `/` `/` `k ` `+` `1` `) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the ` `// above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `n = 7 ; ` ` ` `int` `k = 3 ; ` ` ` ` ` `if` `(n % k == 0) ` ` ` `Console.WriteLine(n / k + ` ` ` `" "` `+ n / k); ` ` ` ` ` `else` ` ` `Console.WriteLine((n - (n % k)) / k + ` ` ` `" "` `+ (((n - (n % k)) / k) + 1)); ` `} ` `} ` ` ` `// This code is contributed ` `// by inder_verama ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the above approach ` ` ` `// Driver code ` `$n` `= 7; ` `$k` `= 3; ` ` ` `if` `(` `$n` `% ` `$k` `== 0) ` ` ` `echo` `$n` `/` `$k` `. ` `" "` `. ` `$n` `/` `$k` `; ` `else` ` ` `echo` `((` `$n` `- (` `$n` `% ` `$k` `)) / ` `$k` `) . ` `" "` `. ` ` ` `(((` `$n` `- (` `$n` `% ` `$k` `)) / ` `$k` `) + 1); ` ` ` `// This code is contributed ` `// by Akanksha Rai(Abby_akku) ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

2 3

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.

## Recommended Posts:

- Number of chocolates left after k iterations
- Number of ways to distribute N Paper Set among M students
- Maximum students to pass after giving bonus to everybody and not exceeding 100 marks
- Puzzle | Selling Chocolates
- Probability of getting two consecutive heads after choosing a random coin among two different types of coins
- Count the number of ways to give ranks for N students such that same ranks are possible
- Minimum LCM and GCD possible among all possible sub-arrays
- Find maximum among x^(y^2) or y^(x^2) where x and y are given
- Maximum points of intersections possible among X circles and Y straight lines
- Python program for arranging the students according to their marks in descending order
- Find maximum product of digits among numbers less than or equal to N
- Maximum LCM among all pairs (i, j) of first N natural numbers
- Count of ways to distribute N items among 3 people with one person receiving maximum
- Maximum GCD among all pairs (i, j) of first N natural numbers
- Find position of the given number among the numbers made of 4 and 7
- Random number generator in arbitrary probability distribution fashion
- Distribution of a Number in Array within a Range
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Standard Normal Distribution (SND) - Java Program
- Cake Distribution Problem

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.