Explanation
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\nO(N^3 + N*2^N)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int minimumIncompatibility(vector<int>& nums, int k) \n    {\n        int n = nums.size(), m = n/k;\n        // bmap contains the value of all acceptable subsets\n        const vector<int> submap = getMap(nums, m);\n        vector<int> dp(1<<n, -1);\n        dp[0] = 0;\n        for (int b = 0; b < (1<<n); b++)\n        {\n            if (__builtin_popcount(b)%m != 0) continue;//b is not a combination of some subsets (maybe 1, 2, ..., k subsets)\n            //iterating thru all b\'s submasks, which is acceptable\n            //ex: b=1011001 -> sub = 1011000, 1010000, 1000000, 0000000\n            for (int sub = b; sub; sub = (sub-1)&b)\n            {\n                if (submap[sub] == -1) continue; // if submask hasnt caculate -> not acceptable\n                if (dp[b-sub] == -1) continue; // if this submask is oke, but it cant combine with other subset to from b\n// dp to find the best permutation of b: give the minimum value\n                if (dp[b] == -1) dp[b] = dp[b-sub] + submap[sub];\n                else dp[b] = min(dp[b], dp[b-sub] + submap[sub]);\n            }\n        }\n        return dp.back();\n    }\nprivate:\n    // Generates a vetor v where:\n    //   v[b] = the incompatibility of bitmask b, if b represents a valid subset of size m\n    //   v[b] = -1, if b is not a valid subset of size m\n    vector<int> getMap(const vector<int>& nums, int m)\n    {\n        int n = nums.size();\n        vector<int> ans(1<<n, -1);\n        for (int mask = 0; mask < (1<<n); mask++)\n        {\n            // mask is a subset\n            // numbers of elements != m -> not a subset\n            if (__builtin_popcount(mask) != m) continue;\n            int flag = 1;\n            int mX = 0, mI = 17, vis = 0;\n            // caculate the value of this subset\n            // find max and min element\n            for (int i = 0; i < n; i++)\n            {\n                // if this subset doesnot contain this index -> continue;\n                if ((mask & (1<<i)) == 0) continue;\n                if (vis & (1<<nums[i])) // if an elements appear twice -> this subset not acceptable\n                {\n                    flag = 0;\n                    break;\n                }\n                vis |= (1<<nums[i]);\n                mX = max(mX, nums[i]);\n                mI = min(mI, nums[i]);\n            }\n            // if this subset not ok -> dont caculate its value\n            if (!flag) continue; \n            ans[mask] = abs(mX - mI);\n        }\n        return ans;\n    }\n};\n```
['C++']