Maximum Number of Groups Entering a Competition - Problem
You are given a positive integer array grades which represents the grades of students in a university. You would like to enter all these students into a competition in ordered non-empty groups, such that the ordering meets the following conditions:
- The sum of the grades of students in the
ithgroup is less than the sum of the grades of students in the(i + 1)thgroup, for all groups (except the last). - The total number of students in the
ithgroup is less than the total number of students in the(i + 1)thgroup, for all groups (except the last).
Return the maximum number of groups that can be formed.
Input & Output
Example 1 — Basic Case
$
Input:
grades = [10,6,12,7,3,5]
›
Output:
3
💡 Note:
Sort to get [3,5,6,7,10,12]. Form groups: [3] (sum=3), [5,6] (sum=11), [7,10,12] (sum=29). Constraints satisfied: 3 < 11 < 29 (sums) and 1 < 2 < 3 (sizes).
Example 2 — Small Array
$
Input:
grades = [8,8]
›
Output:
1
💡 Note:
With only 2 students, we can only form 1 group containing both students. Cannot form 2 groups because that would require the second group to have more students than the first.
Example 3 — Edge Case
$
Input:
grades = [1,2,3,4,5,6]
›
Output:
3
💡 Note:
Already sorted. Optimal grouping: [1] (sum=1), [2,3] (sum=5), [4,5,6] (sum=15). Cannot form 4 groups because we'd need groups of sizes 1,2,3,4 but only have 6 students total.
Constraints
- 1 ≤ grades.length ≤ 105
- 1 ≤ grades[i] ≤ 106
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of student grades: [10,6,12,7,3,5]
2
Sort & Group
Sort grades and form groups: [3] | [5,6] | [7,10,12]
3
Verify
Check constraints: sums 3<11<29, sizes 1<2<3
Key Takeaway
🎯 Key Insight: Sort grades and greedily form groups using smallest available elements to maximize the total number of groups
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code