DP, backtracking
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```python3 []
class Solution:

    def __init__(self):
        self.mem = {}

    def findMin(self, arr, num):
        from itertools import combinations
        n = len(arr)
        if not arr:
            return 0
        if arr in self.mem:
            return self.mem[arr]
        ans = 10 ** 9
        for x in combinations(arr, num):
            st = set(arr)
            a, b = 0, 10 ** 9
            for val, freq in x:
                a = max(a, val)
                b = min(b, val)
                st.remove((val, freq))
                if freq > 1:
                    st.add((val, freq - 1))
            ans = min(ans, a - b + self.findMin(frozenset(st), num))
        self.mem[arr] = ans
        return ans
            
    def minimumIncompatibility(self, nums: list[int], k: int) -> int:
        from collections import defaultdict
        tab = defaultdict(int)
        n = len(nums)
        for i in range(n):
            tab[nums[i]] += 1
        num = n // k
        if any(tab[x] > k for x in tab):
            return -1
        return self.findMin(frozenset(tab.items()), num)
```
['Backtracking', 'Python3']