Sort by

recency

|

1259 Discussions

|

  • + 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";

  • + 0 comments

    Solution from my side for this problem

    def catAndMouse(x, y, z):
        a = abs(x-z)
        b = abs(y-z)
        if(a == b):
            return 'Mouse C'
        elif(a<b):
            return 'Cat A'
        elif(a>b):
            return 'Cat B'
    
  • + 0 comments

    JS, Javascript Solution:-

    function catAndMouse(x, y, z) {
        const aMDistance = Math.abs(x-z)
        const bMDistance = Math.abs(y-z)
        if (aMDistance === bMDistance) return "Mouse C"; else if (aMDistance > bMDistance) return 'Cat B';
        return 'Cat A';
    }