[Solved] Water Challenge with Java, C++

Water Challenge: “Kids on the Move” is a challenging game show organized for kids from age 10 to 15 years. Eddy and Sarah participated at the show and progressed to the semi-finals. At the semi-finals, they were given two vessels, one of which can accommodate a litres of water and the other – b litres of water. The challenge to them was to determine the number of steps required to obtain exactly c litres of water in one of the vessels.

At the beginning both vessels are empty. The following operations are counted as ‘steps’:

  • emptying a vessel,
  • filling a vessel,
  • pouring water from one vessel to the other, without spilling, until one of the vessels is either full or empty.

Help the kids clear the challenge and progress to the finals. Write a method to find the number of steps required to obtain exactly c litres of water in one of the vessels.

Hence create a class named PouringWater with the following method.

Method NameDescription
int minSteps(int,int,int)                                                                                              This recursive method should return the minimum number of steps required to obtain c litres, or return -1 if this is impossible.

Create a driver class called Main. In the Main method, obtain input from the user in the console and display the minimum number of steps required to obtain c litres, or return -1 if this is impossible by calling the minSteps method present in the PouringWater class.

[Note: Strictly adhere to the Object Oriented Specifications given in the problem statement.

All class names, attribute names and method names should be the same as specified in the problem statement.]

Input Format:

There are three lines of the input which contains three positive integers ‘a’, ‘b’ and’c’ respectively.

Output Format:

Output the minimum number of steps required to obtain c litres, or -1 if this is impossible.

Refer sample input and output for formatting specifications.

Sample Input 1:

5

2

3

Sample Output 1:

2

Sample Input 2:

2

3

4

Sample Output 2:

-1

Solution

import java.util.*;

import java.math.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int A = in.nextInt();
        int B = in.nextInt();
        int C = in.nextInt();
              if (C > A && C > B)
        {
            //sauravhathi
            System.out.println(-1);
        }
        else if (C % gcd(A, B) != 0)
        {
            System.out.println(-1);
        }
        else
        {
            System.out.println(Math.min(minSteps(A, B, C), minSteps(B, A, C)));
        }
    }
    public static int minSteps(int A, int B, int C) {
        int move = 1, a = A, b = 0, exch;
        while (a != C && b != C) {
            exch = Math.min(a, B - b);
            //sauravhathi
            b += exch;
            a -= exch;
            move++;
            if (a == C || b == C) {
                break;
            }
            if (a == 0) {
                a = A;
                //sauravhathi
                move++;
            }
            if (b == B) {
                b = 0;
                move++;
            }
        }
        return move;
    }

    public static int gcd(int a, int b) {
        if (a == 0) {
            return b;
        } else {
            //sauravhathi
            return gcd(b % a, a);
        }
    }
}

#include <iostream>
#include <algorithm>
using namespace std;

int minSteps(int, int, int);
int gcd(int, int);

int main()
{

    int A, B, C;
    cin >> A >> B >> C;
    if (C > A && C > B)
    {
        cout << -1 << endl;
    }
    else if (C % gcd(A, B) != 0)
    {
        cout << -1 << endl;
    }
    else
    {
        int res = min(minSteps(A, B, C), minSteps(B, A, C));
        // sauravhathi
        cout << res << endl;
    }
    return 0;
}

int minSteps(int A, int B, int C)
{
    int move = 1, a = A, b = 0, exch;
    //sauravhathi
    while (a != C && b != C)
    {
        exch = min(a, B - b);
        b += exch;
        a -= exch;
        move++;
        if (a == C || b == C)
        {
            break;
        }
        if (a == 0)
        {
            a = A;
            move++;
        }
        if (b == B)
        {
            b = 0;
            move++;
        }
    }
    return move;
}

int gcd(int a, int b)
{
    if (a == 0)
    {
        // sauravhathi
        return b;
    }
    else
    {
        return gcd(b % a, a);
    }
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

πŸ“Œ Nodejs and Android 😎
πŸ“Œ Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *