Students with maximum average score of three subjects
Given a file containing data of student name and marks scored by him/her in 3 subjects. The task is to find the list of students having the maximum average score.
Note: If more than one student has the maximum average score, print any one of them.
Examples:
Input: N = 2, file = {{“Shrikanth”, “20” ,”30″, “10”}, {“Ram”, “100”, “50”, “10”}}
Output: Ram 53
Explanation:
Shrikanth has an average of 20, whereas
Ram has a average of 53. So, Ram has the
maximum average.Input: N = 3, file = {{“Adam”, “50”, “10”, “40”}, {“Rocky”, “100”, “90”, “10”}, {“Suresh”, “10”, “90” ,”100″}}
Output: Rocky 66
Explanation:
Rocky and Suresh both have an average of 66, which is the
highest in the class.
Approach :
- Traverse the file data and store average scores for each student.
- Now, find the maximum average score and search for all the students with this maximum average score.
- Print the maximum average score and names as per the order in the file.
Below is the implementation of the above approach:
C++
// C++ program to find the // list of students having maximum average score #include <bits/stdc++.h> using namespace std; string studentRecord(vector<vector<string> >& S, int N) { // code here int maxi = INT_MIN; string result = "" ; for ( int i = 0; i < N; i++) { int avg = (stoi(S[i][1]) + stoi(S[i][2]) + stoi(S[i][3])) / 3; if (avg > maxi) { maxi = avg; result = S[i][0]; } else if (avg == maxi) { result = result + " " + S[i][0]; } } return result + " " + to_string(maxi); } // Driver code int main() { int N = 2; vector<vector<string>> file = { { "Shrikanth" , "20" , "30" , "10" }, { "Ram" , "100" , "50" , "10" }}; cout << studentRecord(file, N); } |
Java
/*package whatever //do not write package name here */ import java.util.*; class GFG { static String studentRecord(String[][] S, int N) { // code here int maxi = Integer.MIN_VALUE; String result = "" ; for ( int i = 0 ; i < N; i++) { int avg = (Integer.parseInt(S[i][ 1 ]) + Integer.parseInt(S[i][ 2 ]) + Integer.parseInt(S[i][ 3 ]))/ 3 ; if (avg > maxi) { maxi = avg; result = S[i][ 0 ]; } else if (avg == maxi) { result = result + " " + S[i][ 0 ]; } } return result + " " + maxi; } public static void main (String[] args) { int N = 2 ; String[][] file= { { "Shrikanth" , "20" , "30" , "10" }, { "Ram" , "100" , "50" , "10" }}; System.out.println(studentRecord(file, N)); } } // This code is contributed by aadityaburujwale. |
Python3
def studentRecord(S, N): maxi = float ( "-inf" ) result = "" for i in range (N): avg = ( int (S[i][ 1 ]) + int (S[i][ 2 ]) + int (S[i][ 3 ])) / 3 if avg > maxi: maxi = avg result = S[i][ 0 ] elif avg = = maxi: result = result + " " + S[i][ 0 ] print (result + " " + str ( int (maxi))) N = 2 file = [[ "Shrikanth" , "20" , "30" , "10" ], [ "Ram" , "100" , "50" , "10" ]] studentRecord( file , N) # This code is contributed by akashish__ |
C#
// C# program to find the // list of students having maximum average score using System; class GFG { static string studentRecord(String[, ] S, int N) { // code here int maxi = Int32.MinValue; String result = "" ; for ( int i = 0; i < N; i++) { int avg = (Int32.Parse(S[i, 1]) + Int32.Parse(S[i, 2]) + Int32.Parse(S[i, 3])) / 3; if (avg > maxi) { maxi = avg; result = S[i, 0]; } else if (avg == maxi) { result = result + " " + S[i, 0]; } } return result + " " + maxi; } public static void Main( string [] args) { int N = 2; String[, ] file = { { "Shrikanth" , "20" , "30" , "10" }, { "Ram" , "100" , "50" , "10" } }; Console.WriteLine(studentRecord(file, N)); } } // This Code is contributed by karandeep1234 |
Javascript
function studentRecord( S, N) { // code here let maxi =Number.MIN_VALUE; let result = "" ; for (let i = 0; i < N; i++) { let avg = (parseInt(S[i][1]) + parseInt(S[i][2]) + parseInt(S[i][3])) / 3; if (avg > maxi) { maxi = avg; result = S[i][0]; } else if (avg == maxi) { result = result + " " + S[i][0]; } } console.log(result + " " + parseInt(maxi)); } // Driver code let N = 2; let file = [[ "Shrikanth" , "20" , "30" , "10" ], [ "Ram" , "100" , "50" , "10" ]]; studentRecord(file, N); // This code is contributed by garg28harsh. |
Ram 53
Time complexity: O(N) where N is the number of students in the given string array.
Auxiliary space: O(N)
Please Login to comment...