Maximum students to pass after giving bonus to everybody and not exceeding 100 marks in C++ Program

C++Server Side ProgrammingProgramming

In this problem, we are given an array stu[] of size n denoting the marks of students in the class. For each student, the maximum mark is 100 and a student needs 50 marks to pass the exam. Our task is to create a program to find the Maximum students to pass after giving a bonus to everybody and not exceeding 100 marks.

Problem Description − We need to give bonus marks to students to pass but the bonus marks will be given to all students. Our task is to maximize the number of students that can pass the exam by giving the bonus marks but no student should get marks more than 100 after giving a bonus. And then return the maximum number of students passing.

Let’s take an example to understand the problem,

Input

stu[] = {45, 32, 78, 10, 53, 67}

Output

5

Explanation

All students passed are :
45 + 22 = 67
32 + 22 = 54
78 + 22 = 100
53 + 22 = 75
67 + 22 = 89

Solution Approach

To solve the problem, we need to give marks to the student but one thing to consider is the maximum marks of any student should not exceed 100. So the maximum bonus that can be given is

Student with max marks(M) + bonus = 100
Bonus = 100 − M

Then we will add this bonus to students' current marks. If it exceeds 50, students pass. The result will be the count of all such students.

Algorithm

Initialise

passCount = 0;

Step 1

Find the student with maximum marks, maxMarks.

Step 2

Calculate bonus that can be given to all students, bonus = 100 − maxMarks.

Step 3

Loop for i −> 0 to n−1

Step 3.1

if(stu[i] + bonus >= 50), passCount++.

Step 4

return passCount.

Example

Program to illustrate the working of our solution,

 Live Demo

#include<iostream>
using namespace std;
int calcPassCount(int stu[], int n) {
   int maxMarks = stu[0];
   for(int i = 1; i < n; i++){
      if(stu[i] > maxMarks)
      maxMarks = stu[i];
   }
   int bonusMarks = 100 − maxMarks;
   int passCount = 0;
   for(int i=0; i<n; i++) {
      if(stu[i] + bonusMarks >= 50)
      passCount ++;
   }
   return passCount;
}
int main() {
   int stu[] = {45, 32, 78, 10, 53, 67};
   int n = sizeof(stu)/sizeof(stu[0]);
   cout<<"The Maximum students to pass after giving bonus to everybody is "<<calcPassCount(stu, n);
   return 0;
}

Output

The Maximum students to pass after giving bonus to everybody is 5
raja
Updated on 09-Dec-2020 12:36:44

Advertisements