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
kelements. - All elements in each group are distinct.
- Each element in
numsmust be assigned to exactly one group.
Return true if such a partition is possible, otherwise return false.
Example 1
Input
nums = [1,2,3,4], k = 2Output
trueOne 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
Input
nums = [3,5,2,2], k = 2Output
trueOne 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