Maximum Average Pass Ratio - Problem

There is a school that has classes of students and each class will be having a final exam. You are given a 2D integer array classes, where classes[i] = [passi, totali]. You know beforehand that in the ith class, there are totali total students, but only passi number of students will pass the exam.

You are also given an integer extraStudents. There are another extraStudents brilliant students that are guaranteed to pass the exam of any class they are assigned to. You want to assign each of the extraStudents students to a class in a way that maximizes the average pass ratio across all the classes.

The pass ratio of a class is equal to the number of students of the class that will pass the exam divided by the total number of students of the class. The average pass ratio is the sum of pass ratios of all the classes divided by the number of the classes.

Return the maximum possible average pass ratio after assigning the extraStudents students. Answers within 10⁻⁵ of the actual answer will be accepted.

Input & Output

Example 1 — Basic Assignment
$ Input: classes = [[1,2],[3,5]], extraStudents = 2
Output: 0.675
💡 Note: Initially: Class 1 has ratio 1/2=0.5, Class 2 has ratio 3/5=0.6. The improvement from adding one student to Class 1 is (2/3-1/2)=1/6≈0.167, while for Class 2 it's (4/6-3/5)=1/15≈0.067. So we add the first student to Class 1: [2,3]. The new improvements are (3/4-2/3)=1/12≈0.083 for Class 1 and 1/15≈0.067 for Class 2. Class 1 still has better improvement, so we add the second student there too: [3,4]. Final ratios: 3/4=0.75 and 3/5=0.6. Average = (0.75+0.6)/2 = 0.675.
Example 2 — Single Class
$ Input: classes = [[2,4]], extraStudents = 1
Output: 0.6
💡 Note: Initially: Class has ratio 2/4 = 0.5. Adding one student gives (3,5) with ratio 3/5 = 0.6. Since there's only one class, the average is 0.6.
Example 3 — No Improvement Needed
$ Input: classes = [[3,3],[4,4]], extraStudents = 2
Output: 1.0
💡 Note: Both classes already have 100% pass rate. Adding students maintains 100% pass rate: (4,4) and (5,5) both have ratio 1.0. Average remains 1.0.

Constraints

  • 1 ≤ classes.length ≤ 105
  • classes[i].length = 2
  • 1 ≤ passi ≤ totali ≤ 105
  • 1 ≤ extraStudents ≤ 105

Visualization

Tap to expand
Maximum Average Pass Ratio INPUT classes = [[1,2],[3,5]] Class 0 Pass: 1 Total: 2 Ratio: 0.50 Class 1 Pass: 3 Total: 5 Ratio: 0.60 Extra Students extraStudents = 2 S1 S2 Brilliant students (guaranteed pass) Initial Avg Ratio: (0.5 + 0.6) / 2 = 0.55 ALGORITHM STEPS 1 Build Max Heap by gain = (p+1)/(t+1) - p/t Heap ordered by potential gain: Class 0 gain: 2/3-1/2 = 0.167 Class 1 gain: 4/6-3/5 = 0.067 2 Pop max gain class Add 1 student to it 3 Update and push back Recalculate gain, reinsert 4 Repeat for all extra Until extraStudents = 0 Iterations: i=1: Add to Class0 [2,3] New ratio: 0.667 i=2: Add to Class0 [3,4] New ratio: 0.750 FINAL RESULT Final Class States: Class 0 [3, 4] +2 students 0.750 Class 1 [3, 5] unchanged 0.600 Average Calculation: (0.750 + 0.600) / 2 = 1.350 / 2 = 0.675 OUTPUT 0.78333 (rounded to 5 decimals) OK - Greedy optimal allocation achieved! Key Insight: The gain from adding a student to a class decreases as more students are added. Using a max-heap ensures we always assign the next student to the class where it provides the maximum improvement in pass ratio. Time: O(E * log N), Space: O(N) TutorialsPoint - Maximum Average Pass Ratio | Greedy with Priority Queue
Asked in
Google 28 Facebook 15 Amazon 12 Microsoft 8
18.5K Views
Medium Frequency
~25 min Avg. Time
892 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