PROBLEM DESCRIPTION
You are given a 2D string array A of dimensions N x 2, where each row consists of two strings: first is the name of the student, second is their marks. You have to find the maximum average mark. If it is a floating point, round it down to the nearest integer less than or equal to the number.
SOLUTION
This is pretty simple problem. It becomes easier if we create a Student class to store the details needed to calculate the average later.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class Solution {
public int highestScore(String[][] A) {
int n = A.length;
Map<String, Student> map = new HashMap<>();
for(int i=0; i<n; i++){
String studentName = A[i][0];
Double studentMarks = Double.parseDouble(A[i][1]);
if(map.containsKey(studentName)){
Student student = map.get(studentName);
student.marks = student.marks + studentMarks;
student.subjectCount = student.subjectCount + 1;
}else{
Student student = new Student();
student.name = studentName;
student.marks = studentMarks;
student.subjectCount = 1;
map.put(studentName, student);
}
}
int highestAverage = 0;
for(Student student : map.values()){
Double totalMarks = student.marks;
int subjects = student.subjectCount;
int averageMarks = (int)(totalMarks/subjects);
highestAverage = Math.max(highestAverage, averageMarks);
}
return highestAverage;
}
}
class Student{
public String name;
public Double marks;
public int subjectCount;
}