How To Solve Number Line Jumps | Kangaroo | HackerRank Problem [ Using Formula ]
Aug 15, 2023
How To Solve Number Line Jumps | Kangaroo | HackerRank Problem [ Using Formula ]
In this video, I have explained hackerrank number line jumps solution algorithm. hackerrank kangaroo problem can be solved by using set data structure. The complexity of kangaroo hackerrank solution is O (1). This hackerrank problem is a part of Practice | Algorithms | Implementation | Kangaroo hackerrank challenge. For simplicity, I have divided this hackerrank tutorial into 3 parts. [00:00] Understanding the problem statement. [03:31] Building the logic to solve the problem. [11:26] Coding the logic using java (you can use your own preferred programming language). 🔴 DONT CLICK THIS: https://bit.ly/2G4cEuc 👍 Like us on Facebook: https://www.facebook.com/HackerRankSo … 💎Share this video with a YouTuber friend:    • How To Solve Number Line Jumps | Kang…  ✚ Join our community ► 👉 Coding interview preparation group: https://www.facebook.com/groups/codingip 👉 Telegram link: https://t.me/hackerranksolutions 📖 Resources ► 🙋 Problem statement: https://www.hackerrank.com/challenges … 💡 Source code: https://github.com/Java-aid/Hackerran … ✅ Recommended playlists ► 👉 All hackerrank solutions:    • How To Master In Data Structures And …  👋 Let’s Connect ► Git Hub: https://github.com/kanahaiya Twitter: https://twitter.com/Kanahaiyagupta Linked in: https://www.linkedin.com/in/kanahaiya … Facebook: https://www.facebook.com/coolkanahaiya Instagram: https://www.instagram.com/coolkanahaiya #JAVAAID #numberlinejumps #HackerRankSolutions #HackerRankTutorials #HackerRank #JavaAidTutorials #programming #DataStructures #algorithms #coding #competitiveprogramming #java #codinginterview #problemsolving #KanahaiyaGupta #hackerrankchallenges #Implementation
Content
6.71 -> Hello, My dear friend. I am Kanahaiya Gupta
and welcome to my youtube channel. Today,
12.69 -> we are going to solve Kangaroo hackerrank
problem. But before moving ahead make sure
19.43 -> you have subscribed to my youtube channel.
Let's get started guys.
25.01 -> In this problem, you are given two Kangaroos
on a number line ready to jump in a positive
31.55 -> direction.
The first Kangaroo starts at location x1 and
36.61 -> moves at a rate of v1 meter per jump.
The second Kangaroo starts at location x2
44.44 -> and moves at a rate of v2 meter per jump.
You have to figure out a way to get both Kangaroos
51.539 -> at the same location, at the same time, as
a part of the show. If it is possible return
58.999 -> YES otherwise return NO.
Let's go the input format.
64.4 -> A single line of four space-separated integers
denoting the respective values of x1, v1,
70.67 -> x2 and v2.
where x1, v1 are the integers, starting position
77.64 -> and jump distance for kangaroo 1
x2, v2 are integers starting positions and
84.34 -> jump distance for kangaroo 2
So, let's see the sample input guys.
91.719 -> So, this is the sample input. we have given
four integers x1, v1, x2 and v2 and the same
100.42 -> you can see in this diagram also.
This is the x1 which is zero, so zero represent
109.079 -> the x1 position and then we have v1, you can
see this is the v1 which is shown via 3 unit
124.17 -> of distance. then we have x2, which is 4.
This is x2 and then we have v2, so you can
134.81 -> see, this is v2, the 2-unit distance for each
jump.
142.85 -> So, this is what we have given and we have
to figure out whether both the kangaroos will meet
150.15 -> at the same location at the same time. Here
guys, at the same time means an equal number
157.78 -> of jumps.
You can see kangaroo first is reaching after
162.03 -> 4 jumps and kangaroo 2 also reaching after
4 jumps.
166.45 -> So, here time is nothing there are talking
about the jump. So, after how many jumps both
174.77 -> will reach at the same time. Here they are
not interested to know how many jumps exactly,
180.66 -> they are just asking you to tell whether they
will meet or not? with the given x1, v1, x2,
188.4 -> v2 all the parameters.
So here your task is to tell whether both
194.04 -> will be able to meet at the same location
at the same time or not?
198.91 -> Here same time means its same number of jumps.
So, this is the problem guys, Let's understand
205.48 -> this problem in more detail and try to build
logic around it.
211.87 -> I have taken the same example guys which was
mentioned in the hackerrank platform and each
220.16 -> and every parameter x1, v1, x2 and v2 are
represented in a diagram.
226.53 -> x1 is the position of kangaroo 1
v1 is the velocity or the distance which kangaroo
236.07 -> 1 cover in each jump.
x2 is the position of kangaroo 2 and
242.82 -> v2 is the velocity or a distance which is
covered by kangaroo 2 in each jump.
249.89 -> And our task is to identify whether both will
meet at the same location after the same time.
257.23 -> Here same time means after the same number
of jumps.
261.43 -> So, Let's see how we are going to do it guys.
If you remember in school days, we used one
269.74 -> formula, I hope you remember this.
velocity equals to distance upon time.
279.62 -> but here we have velocity, we have distance
but we do not have time. But as I said here
286.8 -> time is nothing it’s just a jump.
So, I am just changing this formula little
292.65 -> bit and making it this. velocity equals to
distance upon jump.
298.52 -> So, if you calculate the distance it will
be v x j.
307.21 -> Now we know guys, we have to make sure both
will reach at a same point. It means both
312.8 -> have to travel the same number of distances.
So, let's assume a distance is something x,
320.159 -> y anything and both should cover the same
distance. So, suppose, I am taking a kangaroo
328.18 -> 1 first.so kangaroo 1 will cover x1 because
it starts from x1 distance plus velocity v1
338.1 -> *j (number of jumps).
So, the distance covered by kangaroo 1 is
349.669 -> x1 from where it starts plus v1*j. So, it
will cover, suppose up to n. it will cover
359.09 -> n distance or it will reach n location. and
the same location the kangaroo 2 should also
367.219 -> reach the same location. So, after x1+v1*j
kangaroo 1 is reaching to the n location and
379.879 -> the kangaroo 2 also should reach the same
location right. So, we can see x1+v1*j must
389.33 -> be equal to x2 which is kangaroo 2 distance,
kangaroo 2 positions where it starts plus
397.699 -> v2 which is kangaroo 2 velocity * j. Because
after the same number of jumps both should
405.889 -> reach the common location which is n. So,
we can make this equal right.
412.189 -> distance travelled by kangaroo 1 and distance
travelled by kangaroo 2 should be equal. Now
417.02 -> we just simplify this equation. Let's see
guys. So, we can see v1j-v2j=x2-x1. If take
423.919 -> j common then v1-v2 equals to x2-x1.
j equal to (x2-x1) divide by (v1-v2).
448.31 -> So, this is the formula guys which we have
received.
454.24 -> so, the number of jumps will be equals to
(x2-x1) divide by (v1-v2).
462.569 -> As is mentioned clearly in the question, the
number of the jump should be equal. both should
470.21 -> be reached at the same location at the same
time. It means the same number of jumps.
476.37 -> And we know jump which is mentioned via j.
It cannot be a floating value. It means jumps
484.669 -> will always be an integer like 1 jump, 2 jumps,
10 jumps, 20,50 but can never be 1.25 jump,
491.629 -> 1.78 like that. So, we have to make sure if
j is an integer value It means both will reach
502.839 -> at the same time. If j is not an integer value
it means they will never reach at the same
510.129 -> time. So how will you check that j is an integer
value?
514.57 -> If you are pretty much good in mathematics
then definitely i hope you spot it. To check
521.57 -> this condition whether j is an integer or
not? there is simple mathematics we have to
527.76 -> use. we know if we divide this (x2-x1) by
(v1-v2). If its pure integer it means the
538.15 -> remainder will be zero.
so, what we have to check, we have to just
542.57 -> check if we divide this quantity by (v1-v2)
remainder should be zero. Or we can say (x2-x1)
553.99 -> in the computer we represent remainder via
modulo (%) operator (v1-v2). It should be
564.13 -> equals to zero. If it equals to zero modulo
or you can say the remainder is equal to zero.
571.34 -> It means both will reach at the same location
at the same time. So, if the remainder we
578.13 -> can say r equals to zero, it means both will
reach at the same time. So, what will be the
584.94 -> condition guys, to solve this problem? which
is very simple.
589.66 -> If the remainder is equals to zero, what is
remainder guys? you have to calculate this.
597.5 -> and I am saying this as a remainder.
if the remainder is equals to zero it means
607.15 -> both will meet at the same location at the
same time so we have to return YES. so, we
614.21 -> can return YES else we can return NO. very
simple guys.
625.93 -> But one more thing which you need to validate
first before taking the remainder. v1 must
632.95 -> always be greater than v2. why?
Because kangaroo 1 starts first and kangaroo
640.25 -> 2 starts later on.
So, kangaroo 1 will be able to catch kangaroo
645.89 -> 2 if the velocity of kangaroo 1 is greater
than v2. if it's not he can never be able
654.04 -> to catch this, kangaroo 2. So, we have to
check if this is the topmost condition guys,
660.58 -> if v1 >v2 so default we will return NO. but
if these two conditions are satisfied. If
671.39 -> v1>v2 and the remainder is equals to zero
then both the kangaroo will meet at the same
677.55 -> location at the same time. I hope guys this
is clear to you. Now we will see the algorithm
684.82 -> in action guys.
so, this is the function guys and this function
688.88 -> is accepting four parameters x1, v1, x2, v2.
As we have just discussed, what we have to
698.14 -> check. we have to just see whether v1>v2 or
not?
704.31 -> after that, we are just checking for the remainder
and this is the same formula guys which we
709.93 -> have just derived.
so, if this is equals to zero it means both
715.48 -> kangaroos will reach at the same location
at the same time. so, we are returning YES
722.31 -> if this condition is not meeting by default,
we are returning NO.
727.53 -> I hope this problem is clear to you guys.
It’s very simple and precise to code.
731.99 -> Let's run this code in a hackerrank platform.
So, you can see guys sample test case got
740.44 -> passed. and now I am submitting this code.
Congratulations guys, you have done a good
751.38 -> job. So now you can see all the test case
got passed. I hope it’s pretty much clear
757.97 -> to you guys and if you need the source code
you can download the same from my git hub
762.86 -> repository and the link is mentioned in the
description box thanks for watching guys and
769.75 -> please don’t forget to like share a comment
and subscribe to my youtube channel.
Source: https://www.youtube.com/watch?v=52R2pLDjUBw