Given an array A[] containing N pairs in the form of (X, Y). Where X and Y denote the student’s name and marks respectively. Then your task is to output the maximum average marks of a student.
Examples:
Input: A[] = {{“Bob”, “87”}, {“Mike”, “35”}, {“Bob”, “52”}, {“Jason”, “35”}, {“Mike”, “55”}, {“Jessica”, “99”}} Output: 99 Explanation: The average marks of each student are as follows:
- Bob: (87+52)/2 = 139/2 = 69
- Mike: (35+55)/2 = 90/2 = 45
- Jason: (35/1) = 35
- Jessica: (99/1) = 99
Input: A[] = {{“Pradeep”, “54”}, { “Neeraj”, “67”}, { “Sheetal”, “34”}} Output: 67 Explanation: It can be verified that the maximum average marks will be 67.
Approach: To solve the problem follow the below idea:
The problem is implementation based and can be solved using HashMap Data-Structure. Store the total marks obtained by each student in a map along with occurrence of that student in A[]. Then the maximum average marks for each student can be calculated using the formula (Total_Marks/ Occurence of student in A[]). Then iterate map to find maximum average marks.
Steps were taken to solve the problem:
- Create a variable let say MaxAvg and initialize it with Int_MIN intially.
- Create an unorder HashMap let say Map.
- Iterate over A[] using loop and follow below-mentioned steps under the scope of loop:
- If (Student doesn’t exists in Map)
- Initialize Key of Map as student’s name and Value as Pair of <Total_Marks, Occurence of student in A>
- Else
- Add marks of current student into stored Pair’s total marks and increment the occurrence.
- Iterate over Map using loop and follow below-mentioned steps:
- Average = (Student’s total marks/ Occurrence of student)
- Update MaxAvg with max of (MaxAvg, Average)
- Return MaxAvg.
Code to implement the approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMaxAverageMarks(vector<pair<string, string> >& A)
{
int maxAvg = INT_MIN;
if (A.empty())
return 0;
unordered_map<string, pair< int , int > > Map;
for ( auto & current_student : A) {
string studentName = current_student.first;
int marks = stoi(current_student.second);
if (Map.find(studentName) == Map.end()) {
Map[studentName] = make_pair(0, 0);
}
Map[studentName].first += marks;
Map[studentName].second += 1;
}
for ( const auto & entry : Map) {
double totalScore = entry.second.first;
int count = entry.second.second;
maxAvg = max(maxAvg, ( int )(totalScore / count));
}
return maxAvg;
}
int main()
{
vector<pair<string, string> > A
= { { "Bob" , "87" }, { "Mike" , "35" },
{ "Bob" , "52" }, { "Jason" , "35" },
{ "Mike" , "55" }, { "Jessica" , "99" } };
cout << findMaxAverageMarks(A) << endl;
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
public class GFG {
static int findMaxAverageMarks(Map<String, String>[] A)
{
int maxAvg = Integer.MIN_VALUE;
if (A.length == 0 )
return 0 ;
Map<String, Pair<Integer, Integer> > map
= new HashMap<>();
for (Map<String, String> currentStudent : A) {
String studentName
= currentStudent.get( "first" );
int marks = Integer.parseInt(
currentStudent.get( "second" ));
if (!map.containsKey(studentName)) {
map.put(studentName, new Pair<>( 0 , 0 ));
}
Pair<Integer, Integer> pair
= map.get(studentName);
pair.first += marks;
pair.second += 1 ;
}
for (Map.Entry<String, Pair<Integer, Integer> >
entry : map.entrySet()) {
double totalScore = entry.getValue().first;
int count = entry.getValue().second;
maxAvg = Math.max(maxAvg,
( int )(totalScore / count));
}
return maxAvg;
}
public static void main(String[] args)
{
Map<String, String>[] A = new Map[] {
Map.of( "first" , "Bob" , "second" , "87" ),
Map.of( "first" , "Mike" , "second" , "35" ),
Map.of( "first" , "Bob" , "second" , "52" ),
Map.of( "first" , "Jason" , "second" , "35" ),
Map.of( "first" , "Mike" , "second" , "55" ),
Map.of( "first" , "Jessica" , "second" , "99" )
};
System.out.println(findMaxAverageMarks(A));
}
static class Pair<A, B> {
A first;
B second;
Pair(A first, B second)
{
this .first = first;
this .second = second;
}
}
}
|
Python3
def find_max_average_marks(A):
max_avg = float ( '-inf' )
if not A:
return 0
student_map = {}
for current_student in A:
student_name = current_student[ 0 ]
marks = int (current_student[ 1 ])
if student_name not in student_map:
student_map[student_name] = ( 0 , 0 )
total_marks, count = student_map[student_name]
student_map[student_name] = (total_marks + marks, count + 1 )
for student, (total_score, count) in student_map.items():
max_avg = max (max_avg, total_score / count)
return int (max_avg)
if __name__ = = "__main__" :
A = [
( "Bob" , "87" ), ( "Mike" , "35" ),
( "Bob" , "52" ), ( "Jason" , "35" ),
( "Mike" , "55" ), ( "Jessica" , "99" )
]
print (find_max_average_marks(A))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static int
FindMaxAverageMarks(List<Tuple< string , string > > A)
{
int maxAvg = int .MinValue;
if (A.Count == 0)
return 0;
Dictionary< string , Tuple< int , int > > Map
= new Dictionary< string , Tuple< int , int > >();
foreach ( var current_student in A)
{
string studentName = current_student.Item1;
int marks = int .Parse(current_student.Item2);
if (!Map.ContainsKey(studentName))
Map[studentName] = Tuple.Create(0, 0);
Tuple< int , int > tuple = Map[studentName];
Map[studentName] = Tuple.Create(
tuple.Item1 + marks, tuple.Item2 + 1);
}
foreach ( var entry in Map)
{
double totalScore = entry.Value.Item1;
int count = entry.Value.Item2;
maxAvg = Math.Max(maxAvg,
( int )(totalScore / count));
}
return maxAvg;
}
public static void Main( string [] args)
{
List<Tuple< string , string > > A
= new List<Tuple< string , string > >{
Tuple.Create( "Bob" , "87" ),
Tuple.Create( "Mike" , "35" ),
Tuple.Create( "Bob" , "52" ),
Tuple.Create( "Jason" , "35" ),
Tuple.Create( "Mike" , "55" ),
Tuple.Create( "Jessica" , "99" )
};
Console.WriteLine(FindMaxAverageMarks(A));
}
}
|
Javascript
function findMaxAverageMarks(A) {
let maxAvg = -Infinity;
if (A.length === 0) {
return 0;
}
const marksMap = new Map();
for (let i = 0; i < A.length; i++) {
const [studentName, marks] = A[i];
if (!marksMap.has(studentName)) {
marksMap.set(studentName, [0, 0]);
}
const [totalMarks, count] = marksMap.get(studentName);
marksMap.set(studentName, [totalMarks + parseInt(marks), count + 1]);
}
for (const [studentName, [totalMarks, count]] of marksMap.entries()) {
maxAvg = Math.max(maxAvg, Math.floor(totalMarks / count));
}
return maxAvg;
}
const A = [
[ "Bob" , "87" ],
[ "Mike" , "35" ],
[ "Bob" , "52" ],
[ "Jason" , "35" ],
[ "Mike" , "55" ],
[ "Jessica" , "99" ]
];
console.log(findMaxAverageMarks(A));
|
Time Complexity: O(N) Auxiliary Space: O(N), As Map is used.
|