top down recursion use 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 {\n    public int minimumIncompatibility(int[] nums, int k) {\n        Integer[] mem = new Integer[1<<nums.length];\n        int min =  min(nums,k,(1<<nums.length)-1,mem);\n        return min==Integer.MAX_VALUE?-1:min;\n    }\n\n    public int min(int[] nums,int k,int mask,Integer[] mem){\n        if(mask==0)\n            return 0;\n        if(mem[mask]!=null)\n            return mem[mask];\n        int min = Integer.MAX_VALUE;\n        a:\n        for(int sub = mask;sub>0;sub = (sub-1)&mask){\n            if(Integer.bitCount(sub)!=nums.length/k)\n                continue;\n\n            TreeSet<Integer> set = new TreeSet<>();\n            for(int i = 0;i<nums.length;i++){\n                if((sub&(1<<i))>0){\n                    if(!set.add(nums[i]))\n                        continue a;\n\n                }\n            }\n            int next = min(nums,k,mask&(~sub),mem);\n            if(next!=Integer.MAX_VALUE){\n                min = Math.min(min,set.last()-set.first()+next);\n            }\n        }\n        mem[mask] = min;\n        return min;\n    }\n}\n```
['Java']