Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Equal 3 Glass of Juices with one Extra unit? What is wrong in my code?

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");
        }
    }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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!!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading