
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Minimum Number of Mails Required to Distribute 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
Advertisements