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.78333
💡 Note: Initially: Class 1 has ratio 1/2=0.5, Class 2 has ratio 3/5=0.6. Adding one student to Class 1 gives (2,3) with ratio 2/3≈0.667. Adding another to Class 2 gives (4,6) with ratio 4/6≈0.667. Average = (0.667+0.667)/2 ≈ 0.667. Actually optimal is adding both to Class 1: (3,4) ratio = 0.75, (3,5) ratio = 0.6, average = 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: Optimize Student DistributionInitial Classes[1, 2]Ratio: 0.5[3, 5]Ratio: 0.6Add 2 Students+2Optimal Result[3, 4]Ratio: 0.75[3, 5]Ratio: 0.6Average Pass Ratio(0.75 + 0.6) / 2 = 0.675
Understanding the Visualization
1
Input Classes
Each class has [pass_count, total_count] and current pass ratio
2
Distribute Students
Assign extra students to classes for maximum improvement
3
Final Average
Calculate the average of all class pass ratios
Key Takeaway
🎯 Key Insight: Always assign students to the class with maximum improvement potential using a greedy approach 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