1681. Minimum Incompatibility
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 minimumIncompatibility(self, nums, k):
        total_elements = len(nums)
        if k == total_elements: return 0
        
        dp_state = [[float('inf')] * total_elements for _ in range(1 << total_elements)]
        nums.sort()
        
        for index in range(total_elements):
            dp_state[1 << index][index] = 0

        for current_mask in range(1 << total_elements):
            indices_in_mask = [i for i in range(total_elements) if current_mask & (1 << i)]
            if len(indices_in_mask) % (total_elements // k) == 1:
                for first, second in permutations(indices_in_mask, 2):
                    dp_state[current_mask][second] = min(dp_state[current_mask][second], dp_state[current_mask ^ (1 << second)][first])
            else:
                for first, second in combinations(indices_in_mask, 2):
                    if nums[first] != nums[second]:
                        dp_state[current_mask][first] = min(dp_state[current_mask][first], dp_state[current_mask ^ (1 << first)][second] + nums[second] - nums[first])

        min_incompatibility = min(dp_state[-1])
        return min_incompatibility if min_incompatibility != float('inf') else -1

```
['Python3']