• + 0 comments

    Will this be optimal?

    function kangaroo(x1, v1, x2, v2) {
            var difference=0;
    
           var calculate=function(){
               if((x1<x2)&&((v1<v2)||(v1==v2))){
             return "NO"
         }
               else{
    while(Math.sign(difference)!=-1){
        x1+=v1;
        x2+=v2;
        difference=x2-x1;
        if(difference==0){
            return "YES"
        }
    }
            return "NO"
               }
           }();
        return calculate;
    }
    

    Check the difference between the two kangaroos on each jump if they come to become 0 then yes or no if the difference goes to negative-This is the main logic other anomaly(Infinite looping problem) is if both distances are equal from start theyll never produce negative or 0 so print no and see if the second kangaroo is way ahead than the first one from start if yes then print "No"