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
  • Apply
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Number Line Jumps
  5. Discussions

Number Line Jumps

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • pmsanjay2003
    4 months ago+ 0 comments

    Hey everyone, I've come up with a solution to a kangaroo problem and I'm excited to share it with you all. I would greatly appreciate your feedback on my solution, as I'm always looking to improve and learn from others. Feel free to review and provide your thoughts. Thanks in advance!

    public static String kangaroo(int x1, int v1, int x2, int v2) {
        // Write your code here
            int jumps = 0;
            if(x1 > x2 && v1 < v2){
                while (x1 != x2) {
                    x1 = x1 + v1;
                    x2 = x2 + v2;
                    jumps++;
                    if(x2 > x1)
                        return "NO";
                }
                return "YES";
            }
            else if(x2 > x1 && v2 < v1){
                while (x2 != x1) {
                    x1 = x1 + v1;
                    x2 = x2 + v2;
                    jumps++;
                    if(x1 > x2)
                        return "NO";
                }
                return "YES";
            }
            return "NO";
        }
    
    -1|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy