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).
Sample Input 0
2
1 2 3
1 3 2
Sample Output 0
Cat B Mouse C
Cat and a Mouse HackerRank Solution in C
#include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> int main(){ int q; scanf("%d",&q); for(int a0 = 0; a0 < q; a0++){ int x; int y; int z; scanf("%d %d %d",&x,&y,&z); int a = abs(x - z); int b = abs(y - z); if (a < b) printf("Cat A\n"); else if (b < a) printf("Cat B\n"); else printf("Mouse C\n"); } return 0; }
Cat and a Mouse HackerRank Solution in C++
#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <string> #include <bitset> #include <cstdio> #include <limits> #include <vector> #include <climits> #include <cstring> #include <cstdlib> #include <fstream> #include <numeric> #include <sstream> #include <iostream> #include <algorithm> #include <unordered_map> using namespace std; int main(){ int q; cin >> q; for(int a0 = 0; a0 < q; a0++){ int x; int y; int z; cin >> x >> y >> z; int d1 = abs(x - z), d2 = abs(y - z); if (d1 < d2) cout << "Cat A" << endl; else if (d1 > d2) cout << "Cat B" << endl; else cout << "Mouse C" << endl; } return 0; }
Cat and a Mouse HackerRank Solution in Java
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { 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"); if (a<b) System.out.println("Cat A"); if (a>b) System.out.println("Cat B"); } } }
Cat and a Mouse HackerRank Solution in Python
#!/bin/python import sys q = int(raw_input().strip()) for a0 in xrange(q): x,y,z = raw_input().strip().split(' ') x,y,z = [int(x),int(y),int(z)] if abs(x-z) == abs(y-z): print 'Mouse C' elif abs(x-z)<abs(y-z): print 'Cat A' else: print 'Cat B'
Attempt Cat and a Mouse HackerRank Challenge
Link – https://www.hackerrank.com/challenges/cats-and-a-mouse
Next HackerRank Challenge Solution
Link – https://exploringbits.com/forming-a-magic-square-hackerrank-solution/
Aayush Kumar Gupta is the founder and creator of ExploringBits, a website dedicated to providing useful content for people passionate about Engineering and Technology. Aayush has completed his Bachelor of Technology (Computer Science & Engineering) from 2018-2022. From July 2022, Aayush has been working as a full-time Devops Engineer.