How To Solve Number Line Jumps | Kangaroo | HackerRank Problem [ Using Formula ]

Поділитися
Вставка
  • Опубліковано 16 гру 2024

КОМЕНТАРІ • 135

  • @JavaAidTutorials
    @JavaAidTutorials  5 років тому +3

    Hello Coding Lover,
    If you have any doubts or any better approach to solve this problem, let me know in comments.

    • @bandhammanikanta1664
      @bandhammanikanta1664 5 років тому

      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.

    • @ojobabatunde8415
      @ojobabatunde8415 5 років тому

      @@bandhammanikanta1664 Please how did you guys measure the order of your algorithms

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому

      In this solution we are not using any loops , only directly formula. that why the complexity of the solution is Constant O(1).

    • @mkwkdk
      @mkwkdk 4 роки тому

      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

    • @rajendra.4311
      @rajendra.4311 3 роки тому

      @@mkwkdk both are same bro , in coding they used -(x2-x1)%-(v1-v2) ,. Both -ve will cancel

  • @fabriciopatrocinio426
    @fabriciopatrocinio426 4 роки тому +4

    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!

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому +2

      Yup. Sometimes mathematics can save your lot of computation time.

  • @pritamdutta8058
    @pritamdutta8058 4 роки тому +4

    thanks, I never think like this in any problem it open my eyes.

  • @Name-pn5rf
    @Name-pn5rf 4 роки тому +1

    awesome sir!! I saw all the discussions in hackerrank but was never able to understand the formula

  • @eissamohamed8197
    @eissamohamed8197 2 роки тому +1

    , thanks very much, without your explanation i wasn't going to think that it has physics even, i must study pysics, really, 😅,

  • @thetechvity9490
    @thetechvity9490 4 роки тому +3

    seriously awesome explanation thank you so so much

  • @aritradutta5679
    @aritradutta5679 5 років тому +2

    Thanks for the solution...I have been wondering for so long now...to optimize the code.

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому

      Most welcome :)

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому

      @AnimWood You can leave comment on my tutorial will try to clarify your doubt ASAP or connect me on fb for quick response.

    • @Mm-zt1gk
      @Mm-zt1gk 3 роки тому +2

      @@JavaAidTutorials
      what if kangaroo 1 is ahead of 2 on line i.e x1>x2 then
      I think ( V1>v2 )condition will not work

  • @saravananjagadeeesan593
    @saravananjagadeeesan593 3 роки тому +1

    U explained it very well. Thanks for the solution :))))))

  • @modaryaghi
    @modaryaghi 4 роки тому +2

    Thanks, man, it is a brilliant approach.

  • @christosbinos8467
    @christosbinos8467 2 роки тому +3

    This can be summarized in javascript in one line using the ternary operator:
    return v2 < v1 && (x2 - x1) % (v1 - v2) == 0 ? "YES" : "NO";

  • @sathishraj1
    @sathishraj1 4 роки тому +2

    You're doing such an amazing job. Thanks !

  • @rushabhrunwal1541
    @rushabhrunwal1541 4 роки тому +2

    respect man ...

  • @ajaib1313
    @ajaib1313 5 років тому +2

    SUPER ALGORITHM SIR ....
    DO MANY VIDEOS ON DATA STRUCTURES AND ALGORITHMS SIR 😍😍😍💕

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому +1

      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.

  • @GauthamHC99
    @GauthamHC99 4 роки тому +1

    Very nice !! So we should be very good at Maths too!!

  • @melk48111
    @melk48111 4 роки тому +1

    Thanks. Love your videos from NY

  • @sesharaoparitala144
    @sesharaoparitala144 5 років тому +2

    This is near perfect,we can't do any better......

  • @bajishaik4558
    @bajishaik4558 5 років тому +1

    Nice explanation and thank you sir for solution

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому

      Thanks for your feedback 😊
      It really motivates to do more for you all.

  • @abeesabees6876
    @abeesabees6876 3 роки тому +3

    if x2>x1 and v2>v1 :
    return "NO"
    else:
    if v2!=v1 and (x2-x1)%(v1-v2)==0:
    return "YES"
    else:
    return "NO"

  • @ratulmaity4948
    @ratulmaity4948 4 роки тому +1

    Your explanation and logic was absolutely wonderful ♥️

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому

      thank you. 🙂
      If you find it useful, please do share with others.

    • @PIYUSH-lz1zq
      @PIYUSH-lz1zq 3 роки тому

      why checking if(v1>v2) ? what is x2 start before x1 then the condition should be reversed ??

  • @MrHello9991
    @MrHello9991 5 років тому +1

    Thank you so much sir. You made the explanation so easy.

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому +1

      most welcome. :)
      and thanks for your nice feedback.
      If you find this channel helpful, please support us by sharing it with your friends too.
      🙏🙏

  • @bollyjolly483
    @bollyjolly483 5 років тому +2

    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.

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому +1

      Your example does not satisfy the given problem constraint which says x2>x1. so above mentioned scenario will not be possible at all.

    • @bollyjolly483
      @bollyjolly483 5 років тому +2

      @@JavaAidTutorials But this example is given in the problem.

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому +4

      @@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.

    • @bollyjolly483
      @bollyjolly483 5 років тому +1

      @@JavaAidTutorials thanx for info sir.

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому

      most welcome..!!

  • @dheerajkushwaha1657
    @dheerajkushwaha1657 5 років тому +2

    very good explanation kanahaiya. keep on posting :)

  • @neerajaambati9271
    @neerajaambati9271 5 років тому

    Sir please explain student grading and apple and orange problems
    Your explanation is very well..easy to understand your way of teaching

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому

      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.

    • @s.akhtarjoomun263
      @s.akhtarjoomun263 3 роки тому

      Let me know if you still need video tutorials for the problems you mentioned, I can make them for you

  • @codingfreek5737
    @codingfreek5737 5 років тому

    My bad , my algorithm took O(n) time to solve this...thanks buddy

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому

      You should feel good now that you can solve it in constant time after watching this tutorial...:P

  • @gokulnarayan6778
    @gokulnarayan6778 5 років тому +1

    Thanks for the solution...

  • @PIYUSH-lz1zq
    @PIYUSH-lz1zq 3 роки тому

    bhaiya , do you have any seperate playlist of implementation tag of hackerrank ?? or these story based questions ??

  • @gurekodok
    @gurekodok 4 роки тому +1

    Thanks bruda

  • @mihirvora391
    @mihirvora391 4 роки тому

    Thank you very much. Nice explaination ❤

  • @rhushikesh
    @rhushikesh 5 років тому +1

    Good Logic

  • @lime_crow
    @lime_crow 2 роки тому

    Thanks buddy

  • @Dhairya5436
    @Dhairya5436 5 років тому

    Very well explained. Thanks.

  • @srikanthr5935
    @srikanthr5935 4 роки тому

    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
      @JavaAidTutorials  4 роки тому

      here they will meet at same distance on number line means they both will meet at fixed point.as we know x1

    • @srikanthr5935
      @srikanthr5935 4 роки тому

      @@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;

  • @swatigupta5586
    @swatigupta5586 3 роки тому +1

    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?

  • @PIYUSH-lz1zq
    @PIYUSH-lz1zq 3 роки тому

    why checking if(v1>v2) ? what is x2 start before x1 then the condition should be reversed ??

  • @pabitrapati6765
    @pabitrapati6765 5 років тому +1

    Thanks for the nice explanation. But the video is too long because of repeatation of the same thing again and again.

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому

      Thanks pabitra for your feedback.
      I am working on it.

  • @anthonygreen2307
    @anthonygreen2307 2 роки тому

    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.

  • @sandeepbhat8933
    @sandeepbhat8933 3 роки тому

    nice

  • @kiranforcoding
    @kiranforcoding 5 років тому +1

    I liked it

  • @shubhsagar6234
    @shubhsagar6234 4 роки тому

    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 :(

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому

      In starting its normal , once you have done good amount of practice then every problem will be easy for you.

    • @khairulislamanik
      @khairulislamanik 3 роки тому

      Yeah, same here. I guess some level of math knowledge is really necessary for competitive programming

  • @devkumar9889
    @devkumar9889 3 роки тому

    I was very never to this q but never thought to use simple phy 😂

  • @ashfaqulislam5606
    @ashfaqulislam5606 5 років тому +1

    nice video

  • @codingnightmares3407
    @codingnightmares3407 4 роки тому

    Why is j on both sides of the equation equal? Shouldn't we consider j1 and j2 seperately?

    • @abdul92643933
      @abdul92643933 4 роки тому +1

      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.

    • @codingnightmares3407
      @codingnightmares3407 4 роки тому

      @@abdul92643933 Thanks

    • @s.akhtarjoomun263
      @s.akhtarjoomun263 3 роки тому

      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)

  • @prakyathkini5540
    @prakyathkini5540 4 роки тому +1

    SIR, IF WE TAKE X1=5,V1=1. X2=2,V2=2, SINCE V1

  • @mathiazhagansampath4697
    @mathiazhagansampath4697 5 років тому

    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.

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому

      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?

  • @neerajaambati9271
    @neerajaambati9271 5 років тому

    Plz upload student grading and apple and orange problems .. my humble request ..

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому

      Will check and let you know..if there is demand for this problem or it teaches something new will make the tutorial on it.

    • @s.akhtarjoomun263
      @s.akhtarjoomun263 3 роки тому

      Hi... Have you got your tutorial? If not, I can make one for you.

  • @abdullaahmad5503
    @abdullaahmad5503 4 роки тому

    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

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому

      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.

  • @nilanjanroy9356
    @nilanjanroy9356 4 роки тому

    Please solve the hackerrank medium level problem

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому

      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

  • @akashbhandary3799
    @akashbhandary3799 Рік тому

    i don't understand how it should be zero all the time?, it can be any int right

  • @lavanyak7911
    @lavanyak7911 4 роки тому

    Can u help u me understand how the value of 'j' remains constant?

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому

      Can you please mention the time as well where algorithm is not clear to you.

    • @lavanyak7911
      @lavanyak7911 4 роки тому

      @@JavaAidTutorials At 4:55 where u say that j = t ...i don't understand how no of jumps are equal to time?

    • @abdul92643933
      @abdul92643933 4 роки тому +1

      @@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.

    • @s.akhtarjoomun263
      @s.akhtarjoomun263 3 роки тому

      j is constant because it is mentioned in the question that both Kangaroos make the same mount of jumps.

  • @pratyushsharma1402
    @pratyushsharma1402 4 роки тому

    Is there any other approach besides this?

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому

      here we have shown the optimal approach but you can always apply the brute force to solve this

  • @gauravjoshi3126
    @gauravjoshi3126 4 роки тому

    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 ???

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому

      I would suggest try pasting your code in comment. somebody should be able to help you

  • @GauthamHC99
    @GauthamHC99 4 роки тому

    console.log(kangaroo( 0, 2, 5, 3)); This should give 'false', but using this code it shows as 'true', please help

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому

      paste your code here may be you would have done some mistake.

    • @GauthamHC99
      @GauthamHC99 4 роки тому

      @Javaaid Yes, I had made an error, its cleared now.. Thank you so much..

  • @KavilJain514
    @KavilJain514 4 роки тому +1

    watch at 1.5x ...😅

  • @bollyjolly483
    @bollyjolly483 5 років тому +1

    where is this thing written that : x1 will always start first ?

    • @JavaAidTutorials
      @JavaAidTutorials  5 років тому

      Have a look on constraints which is given in problem statement, it clearly says x2>x1.

  • @09_rahuldhiman81
    @09_rahuldhiman81 4 роки тому +1

    sir kese kr lete hooo

  • @sasikanthyenugula1661
    @sasikanthyenugula1661 2 роки тому

    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";
    }

  • @RaviKumar-wy9vr
    @RaviKumar-wy9vr 2 роки тому

    what if V2 is greater than V1

  • @TysonEmmy
    @TysonEmmy 4 роки тому

    SIr what if Kangaroo 2 starts before Kangaroo1

  • @mayanksharma3720
    @mayanksharma3720 4 роки тому

    I used your code and all my answers are correct but still, all the test cases are failing.

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому

      please paste your code here, will try to review it.

    • @mayanksharma3720
      @mayanksharma3720 4 роки тому

      @@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";
      }
      }

  • @ahamedsaja6922
    @ahamedsaja6922 4 роки тому +1

    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

    • @JavaAidTutorials
      @JavaAidTutorials  4 роки тому

      Will recommend read out the constraints once, you will get your answer.

  • @ManojKumar-hu6uk
    @ManojKumar-hu6uk 5 років тому +2

    Thanks for the solution.