Program to find minimum number of lectures to attend to maintain 75% in C++


In this problem, we are given two numbers M and N that denote the total number of classes held till present data and the number of classes attended by the student respectively. Our task is to create a program to find minimum number of lectures to attend to maintain 75% in C++.

Problem Description

This is one of the biggest concerns of college students to maintain 75% percent attendance. This program calculates the minimum number of lectures that are to be attended regularly by the student to get an attendance of 75%.

Let’s take an example to understand the problem,

Example 1

Input: M = 32, N = 20

Output: 16

Explanation

To attain a minimum of 75% attendance the student must attend at least 16 lectures. This will make the total lectures 48 and attended lectures 36.

The percentage = 36*100/48 = 75%

Example 1

Input: M = 14, N = 4

Output: 26

Explanation

To attain a minimum of 75% attendance the student must attend at least 26 lectures. This will make the total lectures 40 and attended lectures 30.

The percentage = 30*100/40 = 75%

Solution Approach

Here, we need to find the number of lectures the student needs to attend. One simple way is to add one lecture on both sides and when the division is 0.75 or more than stop adding and return the iterator’s value.

Program to illustrate the working of our solution,

Example

 Live Demo

#include <iostream>
using namespace std;
int maintainAtt(int M, int N) {
   int att = 0; while(1){
      if(((N+att)*100)/(M+att) >= 75){
         return att;
      }
      att++;
   }
}
int main() {
   int M = 23, N = 12;
   cout<<"The total number of lectures to be attended is "<<maintainAtt(M, N);
   return 0;
}

Output−

The total number of lectures to be attended is 21

This approach uses a loop that makes the time complexity of the solution of the order O(n). But we can do it in O(1) time complexity by using the mathematical formula for the count.

Formula for the minimum lecture to attend to maintain 75% attendance is

$$\square\square\square\square\left(\frac{(0.75)+\square-\square}{0.25}\right)$$

Program to illustrate the working of our solution,

Example

 Live Demo

#include <iostream>
#include <math.h>
using namespace std;
int maintainAtt(int M, int N) {
   int att = ceil(((0.75*M) - N)/(0.25)); return att;
}
int main() {
   int M = 30, N = 11;
   cout<<"The total number of lectures to be attended is "<<maintainAtt(M, N);
   return 0;
}

Output

The total number of lectures to be attended is 46

Updated on: 09-Oct-2020

910 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements