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)
Input: N = 3, K = 3, X = 1
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
Input: N = 4, K = 9, X = 2
Refer to the flowchart below
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 recieve 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 recieved is:
Below is the implementation of the above approach:
- Minimum number of given powers of 2 required to represent a number
- Minimum number of palindromes required to express N as a sum | Set 1
- Minimum number of palindromes required to express N as a sum | Set 2
- Minimum number of changes required to make the given array an AP
- Minimum number of operations required to reduce N to 1
- Minimum number operations required to convert n to m | Set-2
- Minimum number of given operation required to convert n to m
- Minimum number of given moves required to make N divisible by 25
- Minimum number of integers required to fill the NxM grid
- Minimum number of bottles required to fill K glasses
- Minimum number of given operations required to reduce the array to 0 element
- Find out the minimum number of coins required to pay total amount
- Minimum number of square tiles required to fill the rectangular floor
- Find the minimum number of operations required to make all array elements equal
- Minimum number of moves required to reach the destination by the king in a chess board
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.