simple 32 line bitmask
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:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int> a;\n    int t1;\n    vector<int> dp;\n    int choose_k(int i,int mask,int mn,int mx,int t,int k){\n        if(t==0 && k==1){\n            return mx-mn;\n        }\n        if(t==0){\n            if(dp[mask]<-1) dp[mask]=choose_k(0,mask,INT_MAX,INT_MIN,t1,k-1);\n            int x=dp[mask];\n            return x==-1?-1: mx-mn+x;\n        }\n        if(i>=a.size()) return -1;\n        if(mask&(1<<i)) return choose_k(i+1,mask, mn,mx,t,k);//already chosen\n        int nxti=i;\n        while(nxti<a.size() && a[nxti]==a[i]) nxti++;//for unique number\n        int m1=-1,m2=-1;\n        m1=choose_k(nxti,mask|(1<<i),min(mn,a[i]),max(mx,a[i]),t-1,k);\n        m2=choose_k(nxti,mask,mn,mx,t,k);//not chosing\n        if(m1==-1 && m2==-1) return -1;\n        if(m1==-1) return m2;\n        if(m2==-1) return m1;\n        return min(m1,m2);\n    }\n    int minimumIncompatibility(vector<int>& nums, int k) {\n        sort(nums.begin(),nums.end());\n        a=nums;\n        if(a.size()%k!=0) return -1;\n        t1=a.size()/k;\n        dp.resize((1<<a.size()),-2);\n        return choose_k(0,0,INT_MAX,INT_MIN,t1,k);\n    }\n};\n```
['C++']