LeetCode in Kotlin

2358. Maximum Number of Groups Entering a Competition

Medium

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:

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:

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:

Solution

class Solution {
    fun maximumGroups(grades: IntArray): Int {
        val len = grades.size
        return (-1 + Math.sqrt(1.0 + 8 * len)).toInt() / 2
    }
}