Maximum Number of Groups Entering a Competition

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 i^th 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 i^th 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.

Example 1
Inputgrades = [10,6,12,7,3,5]
Output3
It is possible to form 3 groups with increasing student counts and grade sums, and it can be shown that more than 3 groups cannot be formed.
Example 2
Inputgrades = [8,8]
Output1
We can only form 1 group, since forming 2 groups would lead to an equal number of students in both groups.

Constraints

  • 1 <= grades.length <= 10^5
  • 1 <= grades[i] <= 10^5

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate