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)
METHOD 2:Using list comprehension and max() function
APPROACH:
The given program takes a list of lists containing student names and their scores in three subjects, and calculates the average score for each student. It then finds the student with the maximum average score and prints their name and average scor
ALGORITHM:
1. Take the number of students and their scores as input.
2. Use a list comprehension to calculate the average score for each student.
3. Find the maximum average score using the max() function.
4. Find the name of the student with the maximum average score.
5. Print the name and score.
C++
#include <iostream> #include <vector> #include <algorithm> #include <string> int main() { // Create a 2D vector to represent the file data std::vector<std::vector<std::string>> file = { { "Shrikanth" , "20" , "30" , "10" }, { "Ram" , "100" , "50" , "10" } }; // Calculate averages and associate them with names std::vector<std::pair< double , std::string>> averages; for ( const auto & scores : file) { double sum = 0; for ( int i = 1; i < scores.size(); i++) { sum += std::stoi(scores[i]); } double average = sum / 3.0; averages.push_back(std::make_pair(average, scores[0])); } // Find the maximum average double maxAverage = -1.0; for ( const auto & entry : averages) { maxAverage = std::max(maxAverage, entry.first); } // Find the name corresponding to the maximum average std::string maxName; for ( const auto & entry : averages) { if (entry.first == maxAverage) { maxName = entry.second; break ; } } // Output the result std::cout << maxName << " " << maxAverage << std::endl; return 0; } |
Java
import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { // Create a 2D List to represent the file data List<List<String>> file = new ArrayList<>(); file.add(List.of( "Shrikanth" , "20" , "30" , "10" )); file.add(List.of( "Ram" , "100" , "50" , "10" )); // Calculate averages and associate them with names List<Pair<Double, String>> averages = new ArrayList<>(); for (List<String> scores : file) { double sum = 0 ; for ( int i = 1 ; i < scores.size(); i++) { sum += Integer.parseInt(scores.get(i)); } double average = sum / 3.0 ; averages.add( new Pair<>(average, scores.get( 0 ))); } // Find the maximum average double maxAverage = - 1.0 ; for (Pair<Double, String> entry : averages) { maxAverage = Math.max(maxAverage, entry.getFirst()); } // Find the name corresponding to the maximum average String maxName = "" ; for (Pair<Double, String> entry : averages) { if (entry.getFirst() == maxAverage) { maxName = entry.getSecond(); break ; } } // Output the result System.out.println(maxName + " " + maxAverage); } } class Pair<A, B> { private final A first; private final B second; public Pair(A first, B second) { this .first = first; this .second = second; } public A getFirst() { return first; } public B getSecond() { return second; } } |
Python3
N = 2 file = [[ "Shrikanth" , "20" , "30" , "10" ], [ "Ram" , "100" , "50" , "10" ]] averages = [( sum ( map ( int , scores[ 1 :])) / 3 , scores[ 0 ]) for scores in file ] max_average = max (averages)[ 0 ] max_name = max (averages)[ 1 ] print (max_name, max_average) |
C#
using System; using System.Linq; using System.Collections.Generic; class Program { static void Main( string [] args) { List<List< string >> file = new List<List< string >> { new List< string > { "Shrikanth" , "20" , "30" , "10" }, new List< string > { "Ram" , "100" , "50" , "10" } }; // Calculate averages and associate with names var averages = file.Select(scores => new List< object > { scores.Skip(1).Select( int .Parse).Sum() / 3.0, scores[0] }).ToList(); // Find the maximum average var maxAverage = averages.Max(x => ( double )x[0]); // Find the name corresponding to the maximum average var maxName = averages.Find(x => ( double )x[0] == maxAverage)[1]; // Output the result Console.WriteLine($ "{maxName} {maxAverage}" ); } } |
Javascript
const N = 2; const file = [[ "Shrikanth" , "20" , "30" , "10" ], [ "Ram" , "100" , "50" , "10" ]]; const averages = file.map(scores => [scores.slice(1).reduce((a, b) => a + parseInt(b), 0) / 3, scores[0]]); const max_average = Math.max(...averages.map(x => x[0])); const max_name = averages.find(x => x[0] === max_average)[1]; console.log(max_name, max_average); |
Ram 53.333333333333336
Time complexity: O(nm), where n is the number of students and m is the number of subjects.
Space complexity: O(n), where n is the number of students.