We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Cats and a Mouse
  5. Discussions

Cats and a Mouse

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 1146 Discussions, By:

recency

Please Login in order to post a comment

  • act_mvalqueza
    15 hours ago+ 0 comments

    int[] dist = {0,0}; dist[0] = Math.Abs(z-x); dist[1] = Math.Abs(z-y); return (dist[0] == dist[1]) ? "Mouse C" : (dist[0] < dist[1]) ? "Cat A" : "Cat B";

    0|
    Permalink
  • bskavitha21
    1 day ago+ 0 comments
    if(abs(x-z)< abs(y-z))
            return "Cat A";
        else if(abs(x-z)> abs(y-z))
            return "Cat B";
        else
            return "Mouse C";
    
    0|
    Permalink
  • mianusman3666
    4 days ago+ 0 comments

    JavaScript

        let a = Math.abs(x-z);
    
        let b = Math.abs(y-z);
    if (a == b){
        return "Mouse C"
    }
    else if (a<=b){
        return "Cat A"
    }
     else if (a>=b){
        return "Cat B"
    }
    
    0|
    Permalink
  • TuanBao
    1 week ago+ 0 comments

    My Java 8 solution, feel free to ask me any questions.

    static String catAndMouse(int x, int y, int z) {
        int catADistance = Math.abs(x - z);
        int catBDistance = Math.abs(y - z);
        
        if(catADistance == catBDistance) {
            return "Mouse C";
        }
        
        if(catADistance > catBDistance) {
            return "Cat B";
        }
        
        return "Cat A";
    }
    
    0|
    Permalink
  • wahibabdul_aw
    2 weeks ago+ 0 comments

    Python3:

    def catAndMouse(x, y, z):
        catADistance = abs(x - z)
        catBDistance = abs(y - z)
        if catADistance == catBDistance: return "Mouse C"
        return "Cat A" if catADistance < catBDistance else "Cat B"    
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy