Given a list of the scores of different students, items, where items[i] = [IDi, scorei] represents one score from a student with IDi, calculate each student's top five average.

Return the answer as an array of pairs result, where result[j] = [IDj, topFiveAveragej] represents the student with IDj and their top five average. Sort result by IDj in increasing order.

A student's top five average is calculated by taking the sum of their top five scores and dividing it by 5 using integer division.

Input & Output

Example 1 — Basic Case
$ Input: items = [[1,91],[1,92],[2,93],[2,97],[1,60],[2,77],[1,65],[1,87],[1,100],[2,100],[2,76]]
Output: [[1,87],[2,88]]
💡 Note: Student 1 has scores [91,92,60,65,87,100]. Top 5 are [100,92,91,87,65], average = (100+92+91+87+65)/5 = 87. Student 2 has scores [93,97,77,100,76]. Top 5 are [100,97,93,77,76], average = (100+97+93+77+76)/5 = 88.
Example 2 — Minimum Scores
$ Input: items = [[1,100],[1,90],[1,80],[2,85],[2,95]]
Output: [[1,54],[2,36]]
💡 Note: Student 1 has only 3 scores [100,90,80], so we pad with zeros: (100+90+80+0+0)/5 = 54. Student 2 has only 2 scores [85,95], so we pad: (95+85+0+0+0)/5 = 36.
Example 3 — Single Student
$ Input: items = [[1,70],[1,80],[1,90],[1,85],[1,95],[1,75]]
Output: [[1,85]]
💡 Note: Student 1 has 6 scores [70,80,90,85,95,75]. Top 5 are [95,90,85,80,75], average = (95+90+85+80+75)/5 = 85.

Constraints

  • 1 ≤ items.length ≤ 1000
  • items[i].length == 2
  • 1 ≤ IDi ≤ 1000
  • 0 ≤ scorei ≤ 100

Visualization

Tap to expand
High Five - Student Top 5 Averages INPUT items = [ID, score] pairs Student 1 [1,91] [1,92] [1,60] [1,65] [1,87] [1,100] Student 2 [2,93] [2,97] [2,77] [2,100] [2,76] Hash Map Structure map[studentID] = [scores] 1: [91,92,60,65,87,100] 2: [93,97,77,100,76] [[1,91],[1,92],[2,93]...] 11 score entries total ALGORITHM STEPS 1 Build Hash Map Group scores by student ID 2 Partial Sort Find top 5 scores per student ID 1 sorted: [100,92,91,87,65,60] ID 2 sorted: [100,97,93,77,76] 3 Calculate Average Sum top 5, divide by 5 ID 1: (100+92+91+87+65)/5 = 435/5 = 87 ID 2: (100+97+93+77+76)/5 = 443/5 = 88 4 Sort by ID Return sorted result pairs FINAL RESULT Top Five Averages by Student Student ID: 1 Top 5 scores: 100, 92, 91, 87, 65 Average: 87 Student ID: 2 Top 5 scores: 100, 97, 93, 77, 76 Average: 88 OUTPUT [[1,87],[2,88]] OK - Sorted by ID Key Insight: Hash Map groups scores by student ID for O(1) lookup. Partial sorting (or using a min-heap of size 5) efficiently finds top 5 scores without fully sorting all scores. Integer division truncates decimals. Time: O(n log n) where n = total scores | Space: O(n) for hash map storage. TutorialsPoint - High Five | Hash Map with Partial Sorting Approach
Asked in
Google 12 Amazon 8 Facebook 6
18.5K Views
Medium Frequency
~15 min Avg. Time
542 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen