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

Number Line Jumps

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 3392 Discussions, By:

recency

Please Login in order to post a comment

  • xXxAboodxXx
    1 day ago+ 0 comments

    JavaScript with O(n) complexity

    function kangaroo(x1, v1, x2, v2) {
        for(let i=0;i<=10000;i++){
            if(x1 === x2){
                return 'YES'
            }
            x1+=v1
            x2+=v2
        }
        return 'NO'
    }
    
    0|
    Permalink
  • skyscreamer
    2 days ago+ 0 comments

    PHP: wanted to cover all possibilities :P

    function kangaroo($x1, $v1, $x2, $v2) {
        // Write your code here
    $pk1 = $x1; //position kangaroo No1
    $pk2 = $x2; //position kangaroo No2
        if ($x1 == $x2 && $v1 != $v2 or
            $x1 != $x2 && $v1 == $v2 or
            $x1 > $x2 && $v1 > $v2 or
            $x1 < $x2 && $v1 < $v2) {
            return "NO";
        }else if ($x1 < $x2) {
            for ($t=1; $pk1<=$pk2; $t++) {
                if (($pk1 +($v1*$t)) == ($pk2 + ($v2*$t))) {
                return "YES";
                } else if (($pk1 +($v1*$t)) > ($pk2 + ($v2*$t))) {
                return "NO";
                }
            }
        }else if ($x2 < $x1) {
            for ($t=1; $pk2<=$pk1; $t++) {
                if (($pk1 +($v1*$t)) == ($pk2 + ($v2*$t))) {
                return "YES";
                } else if (($pk1 +($v1*$t)) < ($pk2 + ($v2*$t))) {
                return "NO";
                }
            }
        } else return "NO";
    }
    
    0|
    Permalink
  • paloma_p1987
    2 days ago+ 0 comments
    def kangaroo(x1, v1, x2, v2):
        if ((x1 > x2 and v1 >= v2) or (x2 > x1 and v2 >= v1)) or ((x1 - x2) %  (v2-v1) != 0) :
            return 'NO'
        return 'YES'
    
    0|
    Permalink
  • driva95
    3 days ago+ 0 comments
    def kangaroo(x1, v1, x2, v2):
        
        # Image the time in an x axis 
        # and position in a y axis
        # kangaroo movements can be represented by two straight lines
        
        if v1 == v2:
            if x1 == x2:
                # They are always moving 
                # in the same locations
                # at the same time
                return "YES"
            else:
                # Same velocity but different initial positions
                return "NO"
        
        num = x2 - x1
        den = v1 - v2
        
        if num / den < 0:
            # Intersection of the two lines happens before x=0 (initial time)
            return "NO"
    
        if num % den == 0:
            # Intersection happens in a specific location
            return "YES"
        
        # Intersection happens, but not in a quantized world made by jumps
        return "NO"
    
    1|
    Permalink
  • jtarazonaj
    4 days ago+ 0 comments

    My solution in Java:

        public static String kangaroo(int x1, int v1, int x2, int v2) {
        return ((v1-v2<=0) || (x2-x1)%(v1-v2)!=0) ? "NO" : "YES";
    }
    
    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