# Cats and a Mouse in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct]

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank, Algorithm Solutions of Problem Solving Section in Java. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for Cats and a Mouse in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

`Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.`

#### Introduction To Algorithm

The word Algorithm means “a process or set of rules to be followed in calculations or other problem-solving operations”. Therefore Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed upon in order to get the expected results.

• It is easy to understand.
• Algorithm is a step-wise representation of a solution to a given problem.
• In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program.

Link for the ProblemCats and a Mouse – Hacker Rank Solution

`Cats and a Mouse – Hacker Rank Solution`

### Problem:

Two cats and a mouse are at various positions on a line. You will be given their starting positions. Your task is to determine which cat will reach the mouse first, assuming the mouse does not move and the cats travel at equal speed. If the cats arrive at the same time, the mouse will be allowed to move and it will escape while they fight.

You are given  queries in the form of , , and  representing the respective positions for cats  and , and for mouse . Complete the function  to return the appropriate answer to each query, which will be printed on a new line.

• If cat  catches the mouse first, print `Cat A`.
• If cat  catches the mouse first, print `Cat B`.
• If both cats reach the mouse at the same time, print `Mouse C` as the two cats fight and mouse escapes.

Example

The cats are at positions  (Cat A) and  (Cat B), and the mouse is at position . Cat B, at position  will arrive first since it is only  unit away while the other is  units away. Return ‘Cat B’.

Function Description

Complete the catAndMouse function in the editor below.

catAndMouse has the following parameter(s):

• int x: Cat ‘s position
• int y: Cat ‘s position
• int z: Mouse ‘s position

Returns

• string: Either ‘Cat A’, ‘Cat B’, or ‘Mouse C’

Input Format

The first line contains a single integer, , denoting the number of queries.
Each of the  subsequent lines contains three space-separated integers describing the respective values of  (cat ‘s location),  (cat ‘s location), and  (mouse ‘s location).

Constraints

Sample Input 0

```2
1 2 3
1 3 2
```

Sample Output 0

```Cat B
Mouse C
```

Explanation 0

Query 0: The positions of the cats and mouse are shown below: Cat  will catch the mouse first, so we print `Cat B` on a new line.

Query 1: In this query, cats  and  reach mouse  at the exact same time: Because the mouse escapes, we print `Mouse C` on a new line.

`Cats and a Mouse – Hacker Rank Solution`
```import java.util.Scanner;

/**
* @author Techno-RJ
*
*/
public class CatsAndAMouse {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
for (int a0 = 0; a0 < q; a0++) {
int x = in.nextInt();
int y = in.nextInt();
int z = in.nextInt();
int a = Math.abs(x - z);
int b = Math.abs(y - z);
if (a == b) {
System.out.println("Mouse C");
} else if (a > b) {
System.out.println("Cat B");
} else if (a < b) {
System.out.println("Cat A");
}

}
in.close();
}
}``` 