Maximum Number of Groups Entering a Competition LeetCode Solution

Problem – Maximum Number of Groups Entering a Competition LeetCode Solution

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.

Example 1:

Input: grades = [10,6,12,7,3,5]
Output: 3
Explanation: The following is a possible way to form 3 groups of students:
- 1st group has the students with grades = [12]. Sum of grades: 12. Student count: 1
- 2nd group has the students with grades = [6,7]. Sum of grades: 6 + 7 = 13. Student count: 2
- 3rd group has the students with grades = [10,3,5]. Sum of grades: 10 + 3 + 5 = 18. Student count: 3
It can be shown that it is not possible to form more than 3 groups.

Example 2:

Input: grades = [8,8]
Output: 1
Explanation: 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 <= 105
  • 1 <= grades[i] <= 105

Maximum Number of Groups Entering a Competition LeetCode Solution in Java

    public int maximumGroups(int[] grades) {
        int k = 0, total = 0, n = grades.length;
        while (total + k + 1 <= n)
            total += ++k;
        return k;
    }

Maximum Number of Groups Entering a Competition LeetCode Solution in C++

    int maximumGroups(vector<int>& grades) {
        int k = 0, total = 0, n = grades.size();
        while (total + k + 1 <= n)
            total += ++k;
        return k;
    }

Maximum Number of Groups Entering a Competition LeetCode Solution in Python

    def maximumGroups(self, A: List[int]) -> int:
        n = len(A)
        k = 0
        while n >= k + 1:
            k += 1
            n -= k
        return k
Maximum Number of Groups Entering a Competition LeetCode Solution Review:

In our experience, we suggest you solve this Maximum Number of Groups Entering a Competition LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Maximum Number of Groups Entering a Competition LeetCode Solution

Find on LeetCode

Conclusion:

I hope this Maximum Number of Groups Entering a Competition LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.