I need help with the following code, I am writing a code to minimize the number of groups of the ages of children at a party, that is the difference between the ages of any two children in a group is supposed to be 0/1.
Example:
total number of children = 13
ages of the children = [2,2,2,3,3,3,4,4,4,5,5,6,7]
output:
[2,2,2,3,3,3] - group1
[4,4,4,5,5]- group2
[6,7] - group3
We don't have to print all three arrays, the number of groups will suffice, kindly check my code and tell me if there is something wrong
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of Kids :");
int n = scan.nextInt();
int[] kids = new int[n];
System.out.println("Enter the ages of kids :");
for(int i = 0;i<n;i++) {
kids[i] = scan.nextInt();
}
Arrays.sort(kids);
int groups =0;
int i =0;
int k =0;
int a = 0;
int b =0;
for(i=a;i<kids.length-a;i++) {
for(k=b+1;k<kids.length-b;k++) {
if(kids[k]-kids[i]!=0 || kids[k]-kids[i]!=1) {
groups++;
kids[i] = kids[k];
a++;
b++;
}
}
}
System.out.println(groups);
}