C++ ||  Complete Explanation, with comments
\n\nclass Solution {\npublic:\n    int func(int mask, vector<int> &ValidPermutations, map<int,int> &ValidPValues,vector<int>&dp){\n        if(mask == 0){. // if
\n```\nclass Solution {\npublic:\n    int func(int mask, vector<int> &ValidPermutations, map<int,int> &ValidPValues,vector<int>&dp){\n        if(mask == 0){. // if all elements of numbers is covered\n            return 0; \n        }\n        if(dp[mask] != -1){\n            return dp[mask]; // this is dp step\n        }\n        int ans = INT_MAX/2;\n        for(auto it : ValidPermutations){\n            if((mask & it) == it){ // wether we can take this permutation\n                int ans1 = func(mask^it,ValidPermutations,ValidPValues,dp); // here we removed this mask such that other permutation can\'t take common elements\n                ans = min(ans,ans1 + ValidPValues[it]);\n            }\n        }\n        return dp[mask] = ans; // dp step\n    }\n    int minimumIncompatibility(vector<int>& nums, int k) {\n        // first will check simple condition for not possible cases\n        int n  = nums.size();\n        if(n%k != 0){\n            return -1;\n        }\n        int cnt = n/k;\n\n        vector<int> ValidPermutations; // to keep track of valid permutations\n        map<int,int> ValidPValues; // to keep track of valid permutation value\n\n        // there can be 2^n permutations as every element has two options\n        // now we are going to check which permutations are valid according to given codition\n        for(int i = 1;i < (1<<n);i++){\n            int count = 0;\n            int mn = INT_MAX; // minimum element which is in this permutation\n            int mx = INT_MIN; // maximum element which is in this permutation\n\n            set<int> st; // to keep track of repeated elements\n            bool isRepeat = false;\n            for(int j = 0;j < n;j++){// itrate on all array elements\n                int mask = 1 << j; // here we are checking this number is in mask or not\n                if(mask & i){\n                    if(st.find(nums[j])  != st.end()){// in this permutation there is element which is repeating\n                        isRepeat = true;\n                        break;\n                    }\n                    count ++;\n                    if(count > cnt){\n                        break;\n                    }\n                    mn = min(mn,nums[j]);\n                    mx = max(mx,nums[j]);\n                    st.insert(nums[j]);\n                }\n            }\n            if(count != cnt){ // if it don\'t pass the count condition\n                continue;\n            }\n            if(isRepeat == false){ // if pass the repatation condtion then will save permutation in validPermutations\n                if(mx == INT_MIN || mn == INT_MAX){\n                    continue;\n                }\n                ValidPermutations.push_back(i);\n                ValidPValues[i] = mx-mn;\n            }\n\n        }\n        vector<int>dp(1<<n,-1);\n        int ans = func((1<<n) -1,ValidPermutations,ValidPValues,dp);\n        if(ans >= INT_MAX/2){\n            return -1;\n        }\n        return ans;\n\n    }\n};\n```
['C++']