[C++] Clean, with detailed explanation
General idea\nEvaluate every valid subset of the given array (where a valid subset is one with no duplicates and has the correct number of elements). For each v
**General idea**\nEvaluate every valid subset of the given array (where a valid subset is one with no duplicates and has the correct number of elements). For each valid subset, find its range (where range = maximum - minimum) + the sum of ranges of all other subsets (precomputed via dp)\n\n**Implementation**\n* Pre-generate the range (max - min) for each subset of nums that doesn\'t have any duplicates\n* Note: to find each subset, you can use bitmasks (i.e. iterate from 1 to (1 << n) - 1, and find how many bits of each bitmask are set to 1, etc.)\n* Then take each subset of nums and apply DP to it as follows:\n\t* Take every sub-subset of this subset and see if it has the right number of elements (== n / k)\n\t* If it does, then dp[subset] = range[sub-subset] + dp[all other elements in subset excluding those in sub-subset]\n\nFor further implementation details, check out the comments in the code. \n\n**Example**\n* Take the example of [1,1,2,3,4,5,5,6] and k = 4. I.e., you need to split the given array into 4 groups of 2 elements each.\n* Consider every subset. Say we are looking at the first 4 elements [1,1,2,3] - which corresponds to a mask of 00001111 (or 11110000 - shouldn\'t matter).\n* Now consider every submask of this mask that has exactly 2 elements. That would correspond to [1,1], [1,2], [1,3], [2, 3]. \n* [1,1] will be rejected as it has duplicates. \n* Next, we take [1,2] (note that [1,2] is a submask of 1010 on the 4 element array [1,1,2,3] from the prior step). dp[[1,1,2,3]] = range[[1,2]] + dp[[1,3]]. Here, range[1,2] = 2 - 1 = 1 and dp[1,3] would have been calculated already since our DP is being built incrementally.\n\n```\nconst int INF = 1e9;\n\nclass Solution {\npublic:\n    int minimumIncompatibility(vector<int>& nums, int k) {\n        int i, n = nums.size(), subset_size = n / k;\n        sort(nums.begin(), nums.end());\n        \n        // Pre-calculate the range (max - min) for each subset of nums\n\t\t// Note that there are (1 << n) subsets (i.e. 2 ^ n subsets) of an array of size n\n        vector<int> range(1 << n, INF);\n        \n        // Each mask represents every possible subset of elements in num (so, "mask" == "subset")\n\t\t// Specifically, if the bit at position i of mask is set to 1, then we include that number in the subset\n        for(int mask = 1; mask < (1 << n); mask++) {\n            int small = -1, big = -1;\n            bool dup = false;\n            \n            // Identify elements that belong to this subset, find the largest & smallest, check for duplicates\n\t\t\t// Recall that this array was sorted in the beginning of the function\n            for(i=0; i<n && !dup; i++)\n                if(mask & (1 << i)) {\n                    if(small == -1)\n                        small = nums[i];\n                    if(big == nums[i])\n                        dup = true;\n                    big = nums[i];\n                }\n            \n            // If no duplicates were found, then calculate and store the range for this subset\n            if(!dup)\n                range[mask] = big - small;\n        }\n        \n        vector<int> dp(1 << n, INF);\n        dp[0] = 0;\n\n        // Iterate over every mask (i.e. subset) and calculate its minimum sum\n        for(int mask = 1; mask < (1 << n); mask++) {\n            \n            // Iterate over every submask for current mask\n            for(int submask = mask; submask; submask = (submask - 1) & mask) {\n                \n                // Check that submask has the right number of elements\n                if(__builtin_popcount(submask) == subset_size) \n                    // Note that      mask = submask + (mask ^ submask)\n                    // ==> i.e.,      mask ^ submask = mask - submask\n                    // In other words, (mask ^ submask) represents those elements in mask that are not in submask\n                    dp[mask] = min(dp[mask], range[submask] + dp[mask ^ submask]);\n            }\n        }\n\t\t// dp.back() == dp[(1 << n) - 1];\n        return dp.back() >= INF ? -1 : dp.back();\n    }\n};\n```
['Dynamic Programming', 'Bit Manipulation', 'C']