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 ith group is less than the sum of the grades of students in the (i + 1)th group, for all groups (except the last).
  • The total number of students in the ith group is less than the total number of students in the (i + 1)th group, 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
Maximum Number of Groups Entering a CompetitionInput grades: [10, 6, 12, 7, 3, 5]10612735After sorting: [3, 5, 6, 7, 10, 12]35671012Group 1Group 2Group 3Size: 1, Sum: 3Size: 2, Sum: 11Size: 3, Sum: 29Constraints satisfied: 3 < 11 < 29 (sums) and 1 < 2 < 3 (sizes)Maximum Groups: 3
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
Asked in
Google 25 Amazon 20 Microsoft 15 Apple 12
23.4K Views
Medium Frequency
~15 min Avg. Time
890 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