Maximum Number of Groups With Increasing Length

You are given a 0-indexed array usageLimits of length n.

Your task is to create groups using numbers from 0 to n - 1, ensuring that each number i is used no more than usageLimits[i] times in total across all groups. You must also satisfy the following conditions:

  • Each group must consist of distinct numbers, meaning that no duplicate numbers are allowed within a single group.
  • Each group except the first one must have a length strictly greater than the previous group.

Return an integer denoting the maximum number of groups you can create while satisfying these conditions.

Example 1
InputusageLimits = [1,2,5]
Output3
Using 0 at most once, 1 at most twice, and 2 at most five times, groups [2], [1,2], and [0,1,2] satisfy the conditions, and 3 is maximum.
Example 2
InputusageLimits = [2,1,2]
Output2
Using 0 at most twice, 1 at most once, and 2 at most twice, groups [0] and [1,2] satisfy the conditions, and 2 is maximum.

Constraints

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

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