Sort by

recency

|

1261 Discussions

|

  • + 0 comments

    Java one-liner:

    static String catAndMouse(int x, int y, int z) {
            return Math.abs(x - z) < Math.abs(y - z) ? "Cat A" : Math.abs(x - z) > Math.abs(y - z) ? "Cat B" : "Mouse C";
        }
    
  • + 0 comments

    Here is my quick and easy Python solution. If the cats are the same distance away, we return "Mouse C", otherwise we return the cat that is closer.

    def catAndMouse(x, y, z):
        if abs(z - y) == abs(z - x):
            return "Mouse C"
        return "Cat A" if abs(z - x) < abs(z - y) else "Cat B"
    
  • + 0 comments

    A Short and Easy C# Solution using Ternary operator With Time and Space Complexity of O(1).

    static string catAndMouse(int x, int y, int z) 
    {
    	int a = Math.Abs(x - z), b = Math.Abs(y - z);
    	return (a < b) ? "Cat A" : (a == b) ? "Mouse C" : "Cat B";
    }
    
  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/vbV5-DqJU74

    string catAndMouse(int x, int y, int z) {
        int a = abs(x-z), b = abs(y-z);
        if(a==b) return "Mouse C";
        if(a < b) return "Cat A";
        return "Cat B";
    }
    

    Short version

    string catAndMouse(int x, int y, int z) {
        int a = abs(x-z), b = abs(y-z);
        return (a > b) ? "Cat B" : (a < b) ? "Cat A" : "Mouse C";
    }
    
  • + 0 comments

    Here is the solution for C#

    int res = Comparer.Default.Compare(Math.Abs(x-z),Math.Abs(y-z));
    return res < 0 ? "Cat A" : res > 0 ? "Cat B" : "Mouse C";