Sort by

recency

|

3961 Discussions

|

  • + 1 comment

    Explanation: we have to print yes iff { x1+v1 x n= x2+v2 x n } we have constraint that x1< x2 so => ( v1-v2 )n=x2-x1 => n=(x2-x1)/(v1-v2)................n must be positive so v1>v2 and n is whole so the remainder should be 0 hence the code is as follows:

    def kangaroo(x1, v1, x2, v2):
        # Write your code here
        if(v1>v2 and ((x2-x1)%(v1-v2)==0)):
            return("YES")
        else:
            return('NO')
            
    
  • + 1 comment

    O(1) Solution

    def kangaroo(x1, v1, x2, v2):
        # Write your code here
        
        if v1 > v2:
            jump_diff = v1 - v2
            position_diff = x2 - x1
            
            if position_diff % jump_diff == 0:
                return "YES"
            return "NO"
        
        
        return "NO"
    
  • + 0 comments
     public static int jump(int startPoint, int distance){
            return startPoint + distance;
        }
        public static String checkRes(int x1, int v1, int x2, int v2){            
            if ((x2 > x1 && v2 >= v1) || (x1 > x2 && v1 >= v2)) { 
                return "NO";
            }
            if ((v1>10000) || (v2>10000)){
                return "NO";
            }
            int res1 = jump(x1, v1);
            int res2 = jump(x2, v2);
            
            if(res1 == res2){
                return "YES";
            }else{
                return checkRes(res1, v1, res2, v2);
            }
        }
        public static String kangaroo(int x1, int v1, int x2, int v2) {
           return checkRes(x1, v1, x2, v2);
    
        }
    
    }
    
  • + 0 comments

    A brute-force approach with max_it equal to maximum size works for all test-cases:

    import os
    
    def kangaroo(x1, v1, x2, v2):
        it = 0
        max_it = 10000
        while x1 != x2:
            x1 += v1
            x2 += v2
            if it == max_it:
                return 'NO'
            it += 1
        return 'YES'
        
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        first_multiple_input = input().rstrip().split()
    
        x1 = int(first_multiple_input[0])
    
        v1 = int(first_multiple_input[1])
    
        x2 = int(first_multiple_input[2])
    
        v2 = int(first_multiple_input[3])
    
        result = kangaroo(x1, v1, x2, v2)
    
        fptr.write(result + '\n')
    
        fptr.close()
    
  • + 0 comments

    My Java 8 Solution

    public static String kangaroo(int x1, int v1, int x2, int v2) {
            if ((x2 > x1 && v2 >= v1) || (x1 > x2 && v1 >= v2)) {
                return "NO";
            } else {
                int diff = Integer.MAX_VALUE, p1 = x1 + v1, p2 = x2 + v2;
                while (true) {
                    if (p1 == p2) {
                        return "YES";
                    } 
                    
                    if (Math.abs(p1 - p2) > diff) {
                        return "NO";
                    }
                    
                    diff = Math.abs(p1 - p2);
                    p1 += v1;
                    p2 += v2;
                }
            }
        }