Sort by

recency

|

3957 Discussions

|

  • + 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;
                }
            }
        }
    
  • + 0 comments

    C++ solution

    string kangaroo(int x1, int v1, int x2, int v2) {
      if ((x1 < x2 && v1 <= v2) || (x2 < x1 && v2 <= v1)) {
        return "NO";
      } else if ((x1 - x2) % (v2 - v1) == 0) {
        return "YES";
      }
      
      return "NO";
    }
    
  • + 0 comments

    def kangaroo(x1, v1, x2, v2): #check if both vectors are same if v1==v2: return 'NO' else: #verify if the intervel where the kangaroos are meeting is grater than 0 as the vectors are moving positively i = math.ceil(((x1-x2)/(v2-v1))) if i > 0 and (x1 + v1*i) == (x2 + i*v2) : return 'YES' else: return "NO"

  • + 0 comments
    def kangaroo(x1, v1, x2, v2):
        # Write your code here
        if v1!=v2:
            K=(x2-x1)/(v1-v2)
        
            if K.is_integer() and K>0:
                return "YES"
            else:
                return "NO"
        else:
            return "NO"
            
    
  • + 0 comments

    Bruh what is this comment search on Hackerrank. Trying to see if x1 always starts left of x2. Then you can calculate it without simulating