Issue
There are three glasses with juice of amount A, B and C units. I have another small empty glass which can hold 1 unit of juice. Using this small glass I can take 1 unit of juice from one glass and pour it to another glass. Can you figure out if they will go to the school peacefully or are they going to start a fight? I promise if there is any possibility for a peaceful morning I will definitely make that happen! Just to clarify, the morning would be peaceful if all those three glasses contain equal amounts of juice, the extra 1 unit glass is empty and during the entire process you do not spill any juice. Also assume that the kids glass can hold any amount of juice.
Note: First one is test cases
int n= sc.nextInt();
int count=0;
int [] arr = new int[3];
for(int i=0; i<n; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
int c= sc.nextInt();
arr[0]=a;
arr[1]=b;
arr[2]=c;
Arrays.sort(arr);
while(arr[0]<=arr[2]) {
arr[2]= arr[2]-1;
arr[0]= arr[0]+1;
if(arr[0]!=arr[2]) {
count--;
if(count>arr[0])
break;
}
else if(arr[0]==arr[2]){
count=1;
break;
}
}
if(count==1) {
System.out.println("Case "+(i+1)+": Peaceful");
}
else {
System.out.println("Case "+(i+1)+": Fight");
}
}
}
Solution
Assuming that the kids glasses have a definite amount of juice poured and we have to distribute the same quantity between the glasses.
You can directly tell if it is going to be peaceful by adding the total amount of the juice in all three glasses and checking if it is multiple of 3 or not. If it is a multiple : peaceful Not a multiple : Fight
Now, To find the minimum number of transfer :
Just find the multiple i.e the total capacity : 12 then multiple : 4
Suppose Glass 1 has min quantity, Glass 2 has max
then, keep transferring Juice from Glass 2 to Glass 1 until anyone of them becomes equal to the multiple. Now, leave the glass with quantity equal to multiple and transfer the amount between other two glasses to make it equal.
Calculate the number of transfers and Voila!!
Answered By - Avi Hritwik
Answer Checked By - Gilberto Lyons (JavaFixing Admin)