Minimum number of mails required to distribute all the questions

Given N questions in a test and K students in the class. Out of the batch of K students, N students memorised exactly one question each. A mail can contain about a maximum of X questions.Â
Find the minimum number of mails required so that the entire class gets to know about all the questions.
NOTE: A mail has the following information- Name of sender, Name of the recipient and the question(s)
Examples:Â Â
Input: N = 3, K = 3, X = 1Â
Output: 6Â
Student 1 sends his question to student 2 and student 3 (2 mails),Â
so does student 2 and student 3 so total mails = 2 * 3 = 6Input: N = 4, K = 9, X = 2Â
Output: 19Â
Refer to the flowchart belowÂ
Â
Flowchart:Â
N = 4, K = 9, X = 2Â
Pivot = 4th StudentÂ
Student 1, 2, & 3 sends 3 mails to student 4. Now student 4 has all the questions. He distributes them accordingly, 3/2 = 2(using ceil) mails to each 3 students who already has 1 question and 4/2 = 2 mails to rest 5 students. So total mails are (3 + 2 * 3 + 2 * 5) = 19Â
Â
Approach: A greedy approach has been used here. A pivot is selected which receives all the questions first and then distribute them accordingly. This will take a minimum number of steps. N-1 students, send each of their questions to the Nth student. So the Nth student has all the questions, (mails sent till now = N-1). Now it is mentioned that the mails contain the name of senders, so the Nth student knows which question came from whom, thus he can avoid sending back the same question. Now the Nth student acts as the distributor, he packages the questions and sends them accordingly. Each of the N-1 students needs to know about the rest of N-1 questions. So minimum mails that needs to be sent to each of them is ceil((N-1)/X), where X is the maximum number of questions a mail can hold and ceil denotes least integer function. So total mails sent till now = ceil((N-1)/X) * (N-1) + (N-1). So N students know about all the questions. The rest of the K-N students needs to know about all the N questions, so each of them must receive atleast ceil(N/X) mails, where X is the maximum number of questions a mail can hold and ceil denotes least integer function. So total mail received is:
Â
Below is the implementation of the above approach:Â
C++
// C++ code to find the// minimum number of mails#include <bits/stdc++.h>#define ll long long int using namespace std;Â
// Function returns the min no of mails requiredlong long int MinimumMail(int n, int k, int x){    // Using the formula derived above    ll m = (n - 1) + (ll)ceil((n - 1) * 1.0 / x) * (n - 1)                      + (ll)ceil(n * 1.0 / x) * (k - n);Â
    return m;}Â
// Driver Codeint main(){    // no of questions    int N = 4;Â
    // no of students    int K = 9;Â
    // maximum no of questions a mail can hold    int X = 2;Â
    // Calling function    cout << MinimumMail(N, K, X) << endl;Â
    return 0;} |
Java
// Java code to find the// minimum number of mailsimport java.io.*;import java.util.*;import java.lang.*;Â
class GFG{     // Function returns the min // no of mails requiredstatic double MinimumMail(int n,                          int k,                           int x){    // Using the formula     // derived above    double m = (n - 1) + Math.ceil((n - 1) * 1.0 / x) * (n - 1)                       + Math.ceil(n * 1.0 / x) * (k - n);Â
    return m;}Â
// Driver Codepublic static void main(String[] args){    // no of questions    int N = 4;Â
    // no of students    int K = 9;Â
    // maximum no of questions    // a mail can hold    int X = 2;Â
    // Calling function    System.out.print((int)MinimumMail(N, K, X) + "\n");}} |
Python3
# Python3 code to find the minimum # number of mailsimport mathÂ
# Function returns the min no of # mails requireddef MinimumMail(n, k, x):         # Using the formula derived above    m = ((n - 1) + int(math.ceil((n - 1) * 1.0 / x) *         (n - 1) + math.ceil(n * 1.0 / x) * (k - n)));Â
    return m;Â
# Driver CodeÂ
# no of questionsN = 4;Â
# no of studentsK = 9;Â
# maximum no of questions # a mail can holdX = 2;Â
# Calling functionprint(MinimumMail(N, K, X));Â
# This code is contributed by mits |
C#
// C# code to find the// minimum number of mailsusing System;Â
class GFG{     // Function returns the min // no of mails requiredstatic double MinimumMail(int n,                          int k,                           int x){    // Using the formula     // derived above    double m = (n - 1) + Math.Ceiling((n - 1) *                           1.0 / x) * (n - 1) +                          Math.Ceiling(n * 1.0 /                                   x) * (k - n);Â
    return m;}Â
// Driver Codepublic static void Main(){    // no of questions    int N = 4;Â
    // no of students    int K = 9;Â
    // maximum no of questions    // a mail can hold    int X = 2;Â
    // Calling function    Console.WriteLine((int)MinimumMail(N, K, X) + "\n");}}Â
// This code is contributed by anuj_67. |
PHP
<?php// PHP code to find the// minimum number of mailsÂ
// Function returns the // min no of mails requiredfunction MinimumMail($n, $k, $x){    // Using the formula    // derived above    $m = ($n - 1) + ceil(($n - 1) *              1.0 / $x) * ($n - 1) +                     ceil($n * 1.0 /                    $x) * ($k - $n);Â
    return $m;}Â
// Driver CodeÂ
// no of questions$N = 4;Â
// no of students$K = 9;Â
// maximum no of questions // a mail can hold$X = 2;Â
// Calling functionecho MinimumMail($N, $K, $X), "\n";Â
// This code is contributed by ajit?> |
Javascript
<script>Â
// Javascript code to find the minimum// number of mailsÂ
// Function returns the min // no of mails requiredfunction MinimumMail(n, k, x){         // Using the formula     // derived above    let m = (n - 1) + Math.ceil((n - 1) * 1.0 / x) *            (n - 1) + Math.ceil(n * 1.0 / x) * (k - n);Â
    return m;}Â
// Driver codeÂ
// No of questionslet N = 4;Â
// No of studentslet K = 9;Â
// Maximum no of questions// a mail can holdlet X = 2;Â
// Calling functiondocument.write(MinimumMail(N, K, X) + "</br>");Â
// This code is contributed by divyesh072019Â
</script> |
19
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!




