
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
- Related Articles
- Minimum number of operations required to delete all elements of the array using C++.
- Program to find minimum number of pins required to hang all banners in C++
- Minimum number of operations required to sum to binary string S using C++.
- Minimum Number of Platforms Required for a Railway Station using C++.
- Minimum number of given operations required to make two strings equal using C++.
- Minimum number of palindromes required to express N as a sum using C++.
- How to find the minimum number of jumps required to reach the end of the array using C#?
- Minimum number of moves to make all elements equal using C++.
- Minimum number of given moves required to make N divisible by 25 using C++.
- Minimum operations required to set all elements of binary matrix in C++
- Minimum number of swaps required to sort an array in C++
- Minimum number of bottles required to fill K glasses in C++
- Minimum operations required to make all the array elements equal in C++
- Program to find minimum number of movie theatres required to show all movies in python
- Program to find minimum number of steps required to catch the opponent in C++

Advertisements