Partition Array Into K-Distinct Groups

You are given an integer array nums and an integer k.

Your task is to determine whether it is possible to partition all elements of nums into one or more groups such that:

  • Each group contains exactly k elements.
  • All elements in each group are distinct.
  • Each element in nums must be assigned to exactly one group.

Return true if such a partition is possible, otherwise return false.

Example 1
Inputnums = [1,2,3,4], k = 2
Outputtrue
One possible partition is [1, 2] and [3, 4], where each group contains k = 2 distinct elements and all elements are used exactly once.
Example 2
Inputnums = [3,5,2,2], k = 2
Outputtrue
One possible partition is [2, 3] and [2, 5], where each group contains k = 2 distinct elements and all elements are used exactly once.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^5
  • ^​​​​​​​1 <= k <= nums.length

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