Maximum students to pass after giving bonus to everybody and not exceeding 100 marks
Given an array which represents the marks of students. The passing grade is and maximum marks that a student can score is , the task is to maximize the student that are passing the exam by giving bonus marks to the students.
Note that if a student is given bonus marks then all other students will also be given the same amount of bonus marks without any student’s marks exceeding . Print the total students that can pass the exam in the end.
Examples:
Input: arr[] = {0, 21, 83, 45, 64}
Output: 3
We can only add maximum of 17 bonus marks to the marks of all the students. So, the final array becomes {17, 38, 100, 62, 81}
Only 3 students will pass the exam.
Input: arr[] = {99, 50, 46, 47, 48, 49, 98}
Output: 4
Approach: Let be the maximum marks of a student among all others then the maximum possible bonus marks that can be given will be . Now for every student whose marks + (100 – M) ? 50, increment the count. Print the count in the end.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int check( int n, int marks[])
{
int * x = std::max_element(marks, marks + n);
int bonus = 100 - ( int )(*x);
int c = 0;
for ( int i = 0; i < n; i++) {
if (marks[i] + bonus >= 50)
c += 1;
}
return c;
}
int main()
{
int n = 5;
int marks[] = { 0, 21, 83, 45, 64 };
cout << check(n, marks) << endl;
return 0;
}
|
C
#include <stdio.h>
int max_element( int arr[], int n)
{
int max = arr[0];
for ( int i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
int check( int n, int marks[])
{
int x = max_element(marks, n);
int bonus = 100 - x;
int c = 0;
for ( int i = 0; i < n; i++) {
if (marks[i] + bonus >= 50)
c += 1;
}
return c;
}
int main()
{
int n = 5;
int marks[] = { 0, 21, 83, 45, 64 };
printf ( "%d\n" , check(n, marks));
return 0;
}
|
Java
import java.util.*;
class GFG{
static int check( int n, List<Integer> marks)
{
Integer x = Collections.max(marks);
int bonus = 100 -x;
int c = 0 ;
for ( int i= 0 ; i<n;i++)
{
if (marks.get(i) + bonus >= 50 )
c += 1 ;
}
return c;
}
public static void main(String[] args)
{
int n = 5 ;
List<Integer> marks = Arrays.asList( 0 , 21 , 83 , 45 , 64 );
System.out.println(check(n, marks));
}
}
|
Python3
def check(n, marks):
x = max (marks)
bonus = 100 - x
c = 0
for i in range (n):
if (marks[i] + bonus > = 50 ):
c + = 1
return c
n = 5
marks = [ 0 , 21 , 83 , 45 , 64 ]
print (check(n, marks))
|
C#
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
class GFG{
static int check( int n, List< int > marks)
{
int x = marks.Max();
int bonus = 100-x;
int c = 0;
for ( int i=0; i<n;i++)
{
if (marks[i] + bonus >= 50)
c += 1;
}
return c;
}
public static void Main()
{
int n = 5;
List< int > marks = new List< int >( new int []{0, 21, 83, 45, 64});
Console.WriteLine(check(n, marks));
}
}
|
PHP
<?php
function check( $n , $marks )
{
$x = max( $marks );
$bonus = 100- $x ;
$c = 0;
for ( $i =0; $i < $n ; $i ++)
{
if ( $marks [ $i ] + $bonus >= 50)
$c += 1;
}
return $c ;
}
$n = 5;
$marks = array (0, 21, 83, 45, 64);
echo check( $n , $marks );
|
Javascript
<script>
function check(n, marks)
{
let x = Math.max(...marks);
let bonus = 100-x;
let c = 0;
for (let i=0; i<n;i++)
{
if (marks[i] + bonus >= 50)
c += 1;
}
return c;
}
let n = 5;
let marks = [0, 21, 83, 45, 64];
document.write(check(n, marks));
</script>
|
Time Complexity: O(n), since the loop runs from 0 to (n – 1).
Auxiliary Space: O(1), since no extra space has been taken.
Last Updated :
29 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...