your logic is not clear to me. let me explain: from 9:09, you said to check if the remainder value is integer value we need to check (x1-x2)%(v2-v1)==0 or not. then suddenly, you r saying if (x1-x2)%(v2-v1) equal to 0 then, kangaroos will reach.. I didnt understand your logic. yes, Your code works. but I didnt understand your explanation.
I was thinking about a solution in mathematics terms first, but unhappily I wasn't able to get in that solution. I need to study more math. Good job, sir!
For example, kangaroo 1 starts at x1=2 with a jump distance v1 =1 and kangaroo 2 starts at x2 = 1 with a jump distance of v2 = 2 . After one jump, they are both at x=3 .so our answer is YES. But according to your code if v1 < v2 , the answer is no , but here the answer is yes.
@@bollyjolly483 you are right in problem statement they have given this example which they should not give, but all test cases are designed based on constraints, as this problem statement is also designed by people like us so there are chances to make mistakes. Will suggest you, always consider constraints over these type of examples.
Hi , I have one query here , as we know s=d/t , so for above problem if i apply this for kangaroo1 => s1 = d1/t1 and for kangaroo2 => s2= d2/t2 , where t1 == t2 since they make same number of jumps hence formula changes as s1 = d1/t and s2 = d2/t , where d1 and d2 are distance covered which cannot be same as x1< x2 so why did we consider d1 and d2 as same as per my understanding it is distance covered not meeting the same destination can you kindly correct with my understanding on this ?
@@JavaAidTutorials Ok got it thank you , then my formula would change something like this : for kangaroo1 : v1 = d1/t where d1 = x+d where x = x2-x1; then for kangaroo2 it would be as x distance is already coveres : v2 = d/t => x+d = v1 * t ; d = (v1 * t ) - x . Similarly for Kangaroo2 => d = v2 * t ;since d = d on equating both expression. (v1 * t) -x = (v2 * t) ; => v1 * t - v2 * t = x where we know x = x2-x replace it here . t(v1-v2) = x2-x1 => t = x2-x1/v1-v2;
Sir I have a doubt that we equalized the formula because the distance will be same but no of jumps will be different for both kangaroos. So why are we taking 'j' (same variable) for both?
8092 x1 starts on 8 and doesn't move. x2 starts on 9 then to 11 , and so on so how does x2 move backward to position 8. the equation answers yes, with 0 remainder . i just don't get the logic of the kangaroo moving back from 9 to 8. thank you for your insight.
Sir, is it normal to stuck on a question whole day as I could not finger this out on my own I was using while loop and x1=x1+v1 on each iteration until x1===x2.... Thank god i found your video... I think competetive programming is not for me :(
j is just a pseudonym for time, t. j increases (time increases) linearly by 1 on both the cases regardless of the other variables. So it can be considered as the same.
j is the same on both sides, because in the question it is mentioned that both Kangaroos make the same amount of jumps. That's it. Kangaroo 1 => (Starting point 0 + 3 steps * 4 jumps = Destination 12) => 0 + (3 * 4) = 12 Kangaroo 2 => (Starting point 4 + 2 steps * 4 jumps = Destination 12) => 4 + (2 * 4) = 12 Since the destination is the same, 0 + (3 * 4) = 4 + (2 * 4) x1 + (v1 * 4) = x2 + (v2 * 4) Since the number of jumps are the same, x1 + (v1 * j) = x2 + (v2 * j) 0 + 3 * j = 4 + (2 * j) 0 + 3j = 4 + 2j 3j - 2j = 4 - 0 j = 4 Since, number of jumps can neither be negative nor a fraction, j has to be an integer to prove that the 2 kangaroos do meet at a certain point... and also v1 has to be greater than v2 (also mentioned in the question)
How the jump will be calculated? " j" will be calculated for given input x1,v1,x2,v2. how you iterate to find the remainder is 0. Can you please explain.
Here, I am not iterating anything as we have only four input x1,v1,x2,v2 just putting these values into the formula, which I have explained and checking if its zero or not?
We have already solved lot of problems on hackerrank of different-2 level. you can visit the channel for more details.- ua-cam.com/users/JavaAidTutorialsvideos
@@lavanyak7911 It's the exact same thing as t. The variable j is used in context to the problem. It's just like saying, Kangaroo X1 can move 3 units in a single second. For example, if Kangaroo X1 has a velocity of 3 units and moved a distance of 6, by using v = d/t, you get t = 2, just like saying 2 jumps with respect to the problem.
I hope we can do like below as well public static String kangarooJumps(int x1, int v1, int x2, int v2) { for (int i = 0; i < Integer.MAX_VALUE; i++) { if ((x1 + (v1 * i)) == (x2 + (v2 * i))) { return "YES at the line number " + (x1 + (v1 * i)); } else { continue; } } return "NO"; }
x1=0 v1=1 x2=0 v2=1 in this case your solution is false my solution : static String kangaroo(int x1, int v1, int x2, int v2) { if(x1==0 &x2==0 && v1==v2) return "yes"; else if(v1
Hello Coding Lover,
If you have any doubts or any better approach to solve this problem, let me know in comments.
your logic is not clear to me.
let me explain: from 9:09, you said to check if the remainder value is integer value we need to check (x1-x2)%(v2-v1)==0 or not. then suddenly, you r saying if (x1-x2)%(v2-v1) equal to 0 then, kangaroos will reach.. I didnt understand your logic. yes, Your code works. but I didnt understand your explanation.
@@bandhammanikanta1664 Please how did you guys measure the order of your algorithms
In this solution we are not using any loops , only directly formula. that why the complexity of the solution is Constant O(1).
the equation of J = X2-X1 /V1-V2 does not address the problem, the correct equation is X1-X2 / V2-V1 = J hence the logic in code
@@mkwkdk both are same bro , in coding they used -(x2-x1)%-(v1-v2) ,. Both -ve will cancel
I was thinking about a solution in mathematics terms first, but unhappily I wasn't able to get in that solution. I need to study more math. Good job, sir!
Yup. Sometimes mathematics can save your lot of computation time.
thanks, I never think like this in any problem it open my eyes.
Glad to help
awesome sir!! I saw all the discussions in hackerrank but was never able to understand the formula
, thanks very much, without your explanation i wasn't going to think that it has physics even, i must study pysics, really, 😅,
seriously awesome explanation thank you so so much
Thanks for the solution...I have been wondering for so long now...to optimize the code.
Most welcome :)
@AnimWood You can leave comment on my tutorial will try to clarify your doubt ASAP or connect me on fb for quick response.
@@JavaAidTutorials
what if kangaroo 1 is ahead of 2 on line i.e x1>x2 then
I think ( V1>v2 )condition will not work
U explained it very well. Thanks for the solution :))))))
Most welcome 😊
Thanks, man, it is a brilliant approach.
Thank you 😊
This can be summarized in javascript in one line using the ternary operator:
return v2 < v1 && (x2 - x1) % (v1 - v2) == 0 ? "YES" : "NO";
You're doing such an amazing job. Thanks !
So nice of you
respect man ...
SUPER ALGORITHM SIR ....
DO MANY VIDEOS ON DATA STRUCTURES AND ALGORITHMS SIR 😍😍😍💕
Sure Ajai.. you can check out my channel I have already uploaded 35+ videos on my channel.
which will help you to learn some tips and tricks.
Very nice !! So we should be very good at Maths too!!
Yes for sure
Thanks. Love your videos from NY
Welcome 😊
This is near perfect,we can't do any better......
Thanks Paritala..:)
Nice explanation and thank you sir for solution
Thanks for your feedback 😊
It really motivates to do more for you all.
if x2>x1 and v2>v1 :
return "NO"
else:
if v2!=v1 and (x2-x1)%(v1-v2)==0:
return "YES"
else:
return "NO"
Your explanation and logic was absolutely wonderful ♥️
thank you. 🙂
If you find it useful, please do share with others.
why checking if(v1>v2) ? what is x2 start before x1 then the condition should be reversed ??
Thank you so much sir. You made the explanation so easy.
most welcome. :)
and thanks for your nice feedback.
If you find this channel helpful, please support us by sharing it with your friends too.
🙏🙏
For example, kangaroo 1 starts at x1=2 with a jump distance v1 =1 and kangaroo 2 starts at x2 = 1 with a jump distance of v2 = 2 . After one jump, they are both at x=3 .so our answer is YES.
But according to your code if v1 < v2 , the answer is no , but here the answer is yes.
Your example does not satisfy the given problem constraint which says x2>x1. so above mentioned scenario will not be possible at all.
@@JavaAidTutorials But this example is given in the problem.
@@bollyjolly483 you are right in problem statement they have given this example which they should not give, but all test cases are designed based on constraints, as this problem statement is also designed by people like us so there are chances to make mistakes.
Will suggest you, always consider constraints over these type of examples.
@@JavaAidTutorials thanx for info sir.
most welcome..!!
very good explanation kanahaiya. keep on posting :)
Thanks Dheeraj.. :)
Sir please explain student grading and apple and orange problems
Your explanation is very well..easy to understand your way of teaching
Will check and let you know..if there is demand for this problem or it teaches something new then will make the tutorial on it.
Let me know if you still need video tutorials for the problems you mentioned, I can make them for you
My bad , my algorithm took O(n) time to solve this...thanks buddy
You should feel good now that you can solve it in constant time after watching this tutorial...:P
Thanks for the solution...
most welcome..and thanks for watching..!!
bhaiya , do you have any seperate playlist of implementation tag of hackerrank ?? or these story based questions ??
Thanks bruda
Thank you very much. Nice explaination ❤
Most welcome 😊
Good Logic
Thanks Rhushikesh...!!
Thanks buddy
Very well explained. Thanks.
thanks for your feedback. 🙂
Hi ,
I have one query here , as we know s=d/t , so for above problem if i apply this for kangaroo1 => s1 = d1/t1 and for kangaroo2 =>
s2= d2/t2 , where t1 == t2 since they make same number of jumps hence formula changes as s1 = d1/t and s2 = d2/t , where d1 and d2 are distance covered which cannot be same as x1< x2 so why did we consider d1 and d2 as same as per my understanding it is distance covered not meeting the same destination can you kindly correct with my understanding on this ?
here they will meet at same distance on number line means they both will meet at fixed point.as we know x1
@@JavaAidTutorials Ok got it thank you , then my formula would change something like this : for kangaroo1 : v1 = d1/t where d1 = x+d where x = x2-x1; then for kangaroo2 it would be as x distance is already coveres : v2 = d/t => x+d = v1 * t ; d = (v1 * t ) - x . Similarly for Kangaroo2 => d = v2 * t ;since d = d on equating both expression. (v1 * t) -x = (v2 * t) ; => v1 * t - v2 * t = x where we know x = x2-x replace it here . t(v1-v2) = x2-x1 => t = x2-x1/v1-v2;
Sir I have a doubt that we equalized the formula because the distance will be same but no of jumps will be different for both kangaroos. So why are we taking 'j' (same variable) for both?
No. of jumps to be same given in the question...........peace out
why checking if(v1>v2) ? what is x2 start before x1 then the condition should be reversed ??
Thanks for the nice explanation. But the video is too long because of repeatation of the same thing again and again.
Thanks pabitra for your feedback.
I am working on it.
8092 x1 starts on 8 and doesn't move. x2 starts on 9 then to 11 , and so on so how does x2 move backward to position 8. the equation answers yes, with 0 remainder . i just don't get the logic of the kangaroo moving back from 9 to 8. thank you for your insight.
nice
I liked it
Thanks buddy..i liked it..😁
Subscribed as well for further updates. Thanks
@@kiranforcoding 👍
Sir, is it normal to stuck on a question whole day as I could not finger this out on my own I was using while loop and x1=x1+v1 on each iteration until x1===x2.... Thank god i found your video... I think competetive programming is not for me :(
In starting its normal , once you have done good amount of practice then every problem will be easy for you.
Yeah, same here. I guess some level of math knowledge is really necessary for competitive programming
I was very never to this q but never thought to use simple phy 😂
nice video
Thank you brother..!!
Good and short logic
@@tarunudainiya6573 thank you.
Why is j on both sides of the equation equal? Shouldn't we consider j1 and j2 seperately?
j is just a pseudonym for time, t. j increases (time increases) linearly by 1 on both the cases regardless of the other variables. So it can be considered as the same.
@@abdul92643933 Thanks
j is the same on both sides, because in the question it is mentioned that both Kangaroos make the same amount of jumps. That's it.
Kangaroo 1 => (Starting point 0 + 3 steps * 4 jumps = Destination 12) => 0 + (3 * 4) = 12
Kangaroo 2 => (Starting point 4 + 2 steps * 4 jumps = Destination 12) => 4 + (2 * 4) = 12
Since the destination is the same, 0 + (3 * 4) = 4 + (2 * 4)
x1 + (v1 * 4) = x2 + (v2 * 4)
Since the number of jumps are the same, x1 + (v1 * j) = x2 + (v2 * j)
0 + 3 * j = 4 + (2 * j)
0 + 3j = 4 + 2j
3j - 2j = 4 - 0
j = 4
Since, number of jumps can neither be negative nor a fraction, j has to be an integer to prove that the 2 kangaroos do meet at a certain point... and also v1 has to be greater than v2 (also mentioned in the question)
SIR, IF WE TAKE X1=5,V1=1. X2=2,V2=2, SINCE V1
The condition should be X1
How the jump will be calculated? " j" will be calculated for given input x1,v1,x2,v2. how you iterate to find the remainder is 0. Can you please explain.
Here, I am not iterating anything as we have only four input x1,v1,x2,v2 just putting these values into the formula, which I have explained and checking if its zero or not?
Plz upload student grading and apple and orange problems .. my humble request ..
Will check and let you know..if there is demand for this problem or it teaches something new will make the tutorial on it.
Hi... Have you got your tutorial? If not, I can make one for you.
i didnt understand the part x1 + v1*j..why did you add x1 and v1*j..x1 is the starting point.. Can you plz explain
because we need to find out the total distance covered by k1 and k2 but need to consider a reference point from where we will calculate.
Please solve the hackerrank medium level problem
We have already solved lot of problems on hackerrank of different-2 level. you can visit the channel for more details.-
ua-cam.com/users/JavaAidTutorialsvideos
i don't understand how it should be zero all the time?, it can be any int right
Can u help u me understand how the value of 'j' remains constant?
Can you please mention the time as well where algorithm is not clear to you.
@@JavaAidTutorials At 4:55 where u say that j = t ...i don't understand how no of jumps are equal to time?
@@lavanyak7911 It's the exact same thing as t. The variable j is used in context to the problem. It's just like saying, Kangaroo X1 can move 3 units in a single second. For example, if Kangaroo X1 has a velocity of 3 units and moved a distance of 6, by using v = d/t, you get t = 2, just like saying 2 jumps with respect to the problem.
j is constant because it is mentioned in the question that both Kangaroos make the same mount of jumps.
Is there any other approach besides this?
here we have shown the optimal approach but you can always apply the brute force to solve this
Sir by this logic I was able to pass in only 12 cases from 30 cases...I have coded in c++, Sir can you please tell me why is it happening with me ???
I would suggest try pasting your code in comment. somebody should be able to help you
console.log(kangaroo( 0, 2, 5, 3)); This should give 'false', but using this code it shows as 'true', please help
paste your code here may be you would have done some mistake.
@Javaaid Yes, I had made an error, its cleared now.. Thank you so much..
watch at 1.5x ...😅
where is this thing written that : x1 will always start first ?
Have a look on constraints which is given in problem statement, it clearly says x2>x1.
sir kese kr lete hooo
😀
I hope we can do like below as well
public static String kangarooJumps(int x1, int v1, int x2, int v2) {
for (int i = 0; i < Integer.MAX_VALUE; i++) {
if ((x1 + (v1 * i)) == (x2 + (v2 * i))) {
return "YES at the line number " + (x1 + (v1 * i));
} else {
continue;
}
}
return "NO";
}
what if V2 is greater than V1
SIr what if Kangaroo 2 starts before Kangaroo1
Look for the given constraints once..😊
I used your code and all my answers are correct but still, all the test cases are failing.
please paste your code here, will try to review it.
@@JavaAidTutorials
string Kangaroo(int x1,int v1,int x2,int v2)
{
int r;
if(v2>v1)
return "No";
else
{
r=(x2-x1)%(v1-v2);
if(r==0)
return "Yes";
else
return "No";
}
}
x1=0 v1=1 x2=0 v2=1 in this case your solution is false
my solution :
static String kangaroo(int x1, int v1, int x2, int v2) {
if(x1==0 &x2==0 && v1==v2) return "yes";
else if(v1
Will recommend read out the constraints once, you will get your answer.
Thanks for the solution.
Thank you.