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.
Examples:Â
Â
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:
Â
CPP
// CPP implementation of the above approach#include <bits/stdc++.h>using namespace std;Â
// Driver codeint 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 |
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} |
Python
# Python implementation of the above approachÂ
n, k = 7, 3if(n % k == 0):Â Â Â Â print(n//k, n//k)Â
else:Â Â Â Â print((n-n % k)//k, (n-n % k)//k + 1) |
C#
// C# implementation of the // above approach using System;Â
class GFG {Â
// Driver codepublic 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 |
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)?> |
Javascript
<script>Â
// JavaScript implementation of the above approach Â
Â
// Driver codevar n = 7 ;var k = 3 ;Â
if (n % k == 0)Â Â Â Â document.write(n / k +" " + n / k);Â
else    document.write((n-(n % k)) / k + " "            + (((n-(n % k))/ k) + 1) ) ;Â
Â
// This code is contributed by 29AjayKumar Â
</script> |
2 3
Â
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



