Skip to content

Practice Minimum Incompatibility

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1681
LeetCode Problem

Minimum Incompatibility

Problem Statement

You are given an integer array nums​​​ and an integer k. You are asked to distribute this array into k subsets of equal size such that there are no two equal elements in the same subset. A subset's incompatibility is the difference between the maximum and minimum elements in that array. Return the minimum possible sum of incompatibilities of the k subsets after distributing the array optimally, or return -1 if it is not possible. A subset is a group integers that appear in the array with no particular order.

Source: Minimum Incompatibility on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [1,2,1,4], k = 2
Output
4

# Example 2

Input
nums = [6,3,8,1,3,1,2,2], k = 4
Output
6

# Example 3

Input
nums = [5,3,3,6,3,3], k = 3
Output
-1

Constraints

1 <= k <= nums.length <= 16
nums.length is divisible by k
1 <= nums[i] <= nums.length

Related LeetCode Problems

Practice more problems similar to Minimum Incompatibility.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.