Java Solution Using Recursion+Backtracking
\nint ans=Integer.MAX_VALUE;\n    \npublic boolean recursionfromright(ArrayList<Integer> list,ArrayList<Integer> mlist,int sz,int ct,int k,int idx,int max,int m
```\nint ans=Integer.MAX_VALUE;\n    \npublic boolean recursionfromright(ArrayList<Integer> list,ArrayList<Integer> mlist,int sz,int ct,int k,int idx,int max,int min,int cans){\n        if(mlist.size()==sz && ct==(k-1)){\n            ans=Math.min(ans,cans+(max-min));   \n            return true;\n        }\n        \n        int csz=mlist.size();\n         int diff=0;\n        if(mlist.size()==sz){\n            mlist=new ArrayList<Integer>();\n            // System.out.println(max+" "+min+" "+list.get(idx));\n            diff=(max-min);  idx=list.size()-1;  max=Integer.MIN_VALUE; min=Integer.MAX_VALUE;\n            \n        }\n        \n        for(int i=idx;i>=0;i--){\n            if(!mlist.contains(list.get(i))){\n                int val=list.get(i);  mlist.add(val);  list.remove(i);\n                if(csz==sz){cans+=diff; ct+=1;}\n                \n                boolean res=recursionfromright(list,mlist,sz,ct,k,i-1,Math.max(max,val),Math.min(min,val),cans);\n                if(res)return true;\n                mlist.remove(mlist.size()-1);  list.add(i,val);\n                \n                if(csz==sz){cans-=diff; ct-=1;}\n            }\n        }\n        \n        return false;\n    }\n    \n    \n    public boolean recursionfromleft(ArrayList<Integer> list,ArrayList<Integer> mlist,int sz,int ct,int k,int idx,int max,int min,int cans){\n        if(mlist.size()==sz && ct==(k-1)){\n            ans=Math.min(ans,cans+(max-min));   \n            return true;\n        }\n        \n        int csz=mlist.size();\n         int diff=0;\n        if(mlist.size()==sz){\n            mlist=new ArrayList<Integer>();\n            diff=(max-min);  idx=0;\n            // ct++;  \n        }\n        \n        for(int i=idx;i<list.size();i++){\n            if(!mlist.contains(list.get(i))){\n                int val=list.get(i);\n                mlist.add(val); list.remove(i);  int omax=max,omin=min;\n                if(csz==sz){cans+=diff; ct+=1;  max=Integer.MIN_VALUE; min=Integer.MAX_VALUE;}\n                \n                boolean res=recursionfromleft(list,mlist,sz,ct,k,i,Math.max(max,val),Math.min(min,val),cans);\n                \n                if(res)return true;\n                mlist.remove(mlist.size()-1);\n                list.add(i,val);\n                if(csz==sz){cans-=diff; ct-=1; max=omax; min=omin;}\n            }\n        }\n        \n        return false;\n    }\n    \n    public int minimumIncompatibility(int[] nums, int k) {\n        HashMap<Integer,Integer> hm=new HashMap<>();\n        \n        for(int i=0;i<nums.length;i++){\n            int freq=hm.getOrDefault(nums[i],0)+1;\n            if(freq>k)return -1;\n            hm.put(nums[i],freq);\n        }\n        \n        int n=nums.length;\n        \n        int size=n/k;\n        \n        Arrays.sort(nums);\n        ArrayList<Integer> list1=new ArrayList<>();\n        ArrayList<Integer> list2=new ArrayList<>();\n        for(int val:nums){list1.add(val); list2.add(val);}\n        \n        recursionfromright(list1,new ArrayList<Integer>(),size,0,k,list1.size()-1,Integer.MIN_VALUE,Integer.MAX_VALUE,0);\n        recursionfromleft(list2,new ArrayList<Integer>(),size,0,k,0,Integer.MIN_VALUE,Integer.MAX_VALUE,0);\n                 \n        return ans;\n    }\n\n```
[]