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

Input: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 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:

## 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 required ` `long` `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 Code ` `int` `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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java code to find the ` `// minimum number of mails ` `import` `java.io.*; ` `import` `java.util.*; ` `import` `java.lang.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function returns the min ` `// no of mails required ` `static` `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 Code ` `public` `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"` `); ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code to find the minimum ` `# number of mails ` `import` `math ` ` ` `# Function returns the min no of ` `# mails required ` `def` `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 questions ` `N ` `=` `4` `; ` ` ` `# no of students ` `K ` `=` `9` `; ` ` ` `# maximum no of questions ` `# a mail can hold ` `X ` `=` `2` `; ` ` ` `# Calling function ` `print` `(MinimumMail(N, K, X)); ` ` ` `# This code is contributed by mits ` |

*chevron_right*

*filter_none*

## C#

`// C# code to find the ` `// minimum number of mails ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function returns the min ` `// no of mails required ` `static` `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 Code ` `public` `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. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP code to find the ` `// minimum number of mails ` ` ` `// Function returns the ` `// min no of mails required ` `function` `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 function ` `echo` `MinimumMail(` `$N` `, ` `$K` `, ` `$X` `), ` `"\n"` `; ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

19

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum increment/decrement operations required on Array to satisfy given conditions
- Top 10 algorithms in Interview Questions | Set 2
- Find minimum moves to bring all elements in one cell of a matrix
- Minimum cost to process m tasks where switching costs
- Minimum steps in which N can be obtained using addition or subtraction at every step
- Split N powers of 2 into two subsets such that their difference of sum is minimum
- Cut all the rods with some length such that the sum of cut-off length is maximized
- Maximum sum by adding numbers with same number of set bits
- Maximize number of elements from Array with sum at most K
- Lead a life problem
- Minimum increment/decrement operations required on Array to satisfy given conditions
- Maximum modified Array sum possible by choosing elements as per the given conditions
- Check if Array forms an increasing-decreasing sequence or vice versa
- Maximize number of elements from Array with sum at most K

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.