Minimum number of mails required to distribute all the questions using C++.



Problem statement

Given N questions in a test and K students in the class. Out of the batch of K students, N students memorized 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

If N = 3, K = 3, X = 1 then one has to send 6 mails −

  • 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 = 6

Algorithm

The final answer can be calculated using the below formula −

ceil(N/X) * (K-N) + (( ceil((N-1)/X)) * (N-1)) + (N-1)

Example

#include <iostream> #include <cmath> using namespace std; int minMailsToBeSent(int n, int k, int x){    int m = (n - 1) + ceil((n - 1) * 1.0 / x) * (n - 1) + ceil(n * 1.0 / x) * (k- n);    return m; } int main(){    int questions = 3;    int students = 3;    int X = 1;    cout << "No of mails to be sent: " << minMailsToBeSent(questions, students, X) << endl;    return 0; }

Output

When you compile and execute the above program. It generates the following output −

No of mails to be sent: 6
Updated on: 2019-10-31T07:04:38+05:30

123 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements