LEETCODE MINIMUM INCOMPATIBILITY 1681
class Solution {
public int minimumIncompatibility(int[] nums, int k) {
int count=0;
int sum2=0;
if(nums.length%2==0){
if(nums.length%k==0){
Arrays.sort(nums);
int n=nums.length;
int m=nums.length/k;
int first=nums[0];
int last=nums[n-1];
int sum=0;
int avg=0;
for(int i=0;i<n;i++){
sum=sum+nums[i];
avg=sum/n;
}
for(int i=1;i<n-1;i++){
/* nums[0]=first;
nums[n-1]=last;*/
while(nums[i+1]<=avg){
if(nums[i]<=nums[i+1]){
int temp=nums[i];
nums[i]=nums[i+1];
nums[i+1]=temp;
}
}
}
ArrayList<Integer> aa=new ArrayList<Integer>();
//int s=nums.length/k;//size of one array
int ar1[][]=new int[n][m];//subset array
for(int i=0;i<n;i=i+m){
for(int j=0;j<m;j++){
ar1[i][j]=nums[j+i];
}
}
for(int i=0;i<n;i=i+m){
for(int j=0;j<m;j++){
System.out.print(ar1[i][j]);
}
System.out.println();
}
for(int i=0;i<n;i++){
aa.add(nums[i]);
}
for(int i=0;i<aa.size()-1;i=i+m){
sum2=sum2+aa.get(i+1)-aa.get(i);
}
System.out.print(sum2);
count++;
}
else
System.out.println("Rejected due to not divisible by k");
}
if(count!=0){
return sum2;
}
else{
System.out.println("Rejected due to size not even");
return -1;
}
}
}
Comments
Post a Comment