HackerRank Cats and a Mouse Problem Solution Yashwant Parihar, April 13, 2023April 14, 2023 In this post, We are going to solve HackerRank Cats and a Mouse 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 q queries in the form of x, y, and representing the respective positions for cats A and B, and for mouse, C. Complete the function Cat and Mouse to return the appropriate answer to each query, which will be printed on a new line. If cat A catches the mouse first, print Cat A. If cat B 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 the mouse escapes. Example X = 2 y = 5 z = 4 The cats are at positions 2 (Cat A) and 5 (Cat B), and the mouse is at position 4. Cat B, at position 5 will arrive first since it is only 1 unit away while the other is 2 units away. Return ‘Cat B’. Function Description Complete the catAndMouse function in the editor below. cat-and-mouse has the following parameter(s): int x: Cat A’s position int y: Cat B’s position int z: Mouse C’s position Returns string: Either ‘Cat A’, ‘Cat B’, or ‘Mouse C’ Input Format The first line contains a single integer, q, denoting the number of queries.Each of the q subsequent lines contains three space-separated integers describing the respective values of x (cat A’s location), y (cat B’s location), and z (mouse C’s location). 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 Cat will catch the mouse first, so we print Cat B on a new line. Query 1: In this query, cats A and B reach C mouse at the exact same time Because the mouse escapes, we print Mouse C on a new line. HackerRank Cats and a Mouse Problem Solution Cats and a Mouse C Solution #include<stdio.h> #include<math.h> #include<stdlib.h> int main(){ int t; scanf("%d",&t); while(t--){ int a,b,c; scanf("%d%d%d",&a,&b,&c); if(abs(c-a)==abs(c-b)) printf("Mouse C\n"); if(abs(c-a)<abs(c-b)) printf("Cat A\n"); if(abs(c-a)>abs(c-b)) printf("Cat B\n"); } } Cats and a Mouse C++ Solution #include <stdio.h> #include <iostream> #include <string> #include <algorithm> using namespace std; int main() { int x, y, z, t; scanf("%d", &t); while(t--) { scanf("%d%d%d", &x, &y, &z); if (abs(x-z) == abs(y-z)) printf("Mouse C\n"); else if(abs(x-z) > abs(y-z)) printf("Cat B\n"); else printf("Cat A\n"); } return 0; } Cats and a Mouse C Sharp Solution using System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution { static void Main(String[] args) { int q = Convert.ToInt32(Console.ReadLine()); for(int a0 = 0; a0 < q; a0++){ string[] tokens_x = Console.ReadLine().Split(' '); int x = Convert.ToInt32(tokens_x[0]); int y = Convert.ToInt32(tokens_x[1]); int z = Convert.ToInt32(tokens_x[2]); int distanceA = Math.Abs(x - z); int distanceB = Math.Abs(y - z); if (distanceA < distanceB) Console.WriteLine("Cat A"); else if (distanceA > distanceB) Console.WriteLine("Cat B"); else Console.WriteLine("Mouse C"); } } } Cats and a Mouse java Solution import java.io.*; import java.util.*; public class Solution { public static String returnSentence; public static String catAndMouse(int catA,int catB,int mouseC){ if(Math.abs(catA-mouseC)>Math.abs(catB-mouseC)){ returnSentence = "Cat B"; }else if (Math.abs(catB-mouseC)> Math.abs(catA-mouseC)){ returnSentence = "Cat A"; }else{ returnSentence = "Mouse C"; } return returnSentence; } public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(int i = 0; i<n;i++){ int firstCat = sc.nextInt(); int secondCat = sc.nextInt(); int mouse = sc.nextInt(); catAndMouse(firstCat,secondCat,mouse); System.out.println(returnSentence); } sc.close(); } } Cats and a Mouse JavaScript Solution process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; process.stdin.on('data', function (data) { input_stdin += data; }); process.stdin.on('end', function () { input_stdin_array = input_stdin.split("\n"); main(); }); function readLine() { return input_stdin_array[input_currentline++]; } /////////////// ignore above this line //////////////////// function main() { var q = parseInt(readLine()); for(var a0 = 0; a0 < q; a0++){ var x_temp = readLine().split(' '); var x = parseInt(x_temp[0]); var y = parseInt(x_temp[1]); var z = parseInt(x_temp[2]); var a = Math.abs(x-z); var b = Math.abs(y-z); if (a == b) { console.log('Mouse C'); } else if (a > b) { console.log('Cat B'); } else { console.log('Cat A'); } } } Cats and a Mouse Python Solution x=int(input()) l=[] for x in range(x): l=list(map(int,input().split())) cat1=l[0] cat2=l[1] rat=l[2] s=abs(rat-cat1) s1=abs(rat-cat2) if(s==s1): print("Mouse C") elif(s>s1): print("Cat B") else: print("Cat A") Other Solutions HackerRank Forming a Magic Square Solution HackerRank Picking Numbers Problem Solution c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython