HackerRank-Easy-Number Line Jumps(TS)

Clement
2 min readJul 8, 2022

--

Alright I cheated on this one, I’ll tell you that. I didn’t come up with the solutiuon completely by myself. I took a peek at the discussion section for some inspiration after sereval attemps. But only the first comment in discussion section.

Anyway, it’s actually easier than we imagined probably. So, here is the situation. 2 kangaroos, one at position x1, one at position x2, are jumping in the positive direction on a number line with different velocity, one with v1, one with v2. We need to write a function that will determine whether they will meet one day.

constraints0 <= x1 < x2 <= 10000
1 <= v1 <= 10000
1 <= v2 <= 10000

Here is my solution and I’ll explain why.

Basically, we need to solve the equation. If kangaroos will meet, than

x1 + jumpCount * v1 === x2 + jumpCount* v2

which means jumpCount will be

jumpCount === (x2 - x1) / (v1 - v2)

Besides, if jumpCount could really exist, if should be over 0 and an integer. To check that, we could use % , which will return the remainder. If the remainder is 0, than jumpCount is an interger.

By the wat, I’m using the ternary operator here, you could look it up if you need to. Google is a good friend and your mentor.

That’s all for today.

--

--

Clement

A web frontend developer’s clueless notes that might contain something about Angular, typescript or other frontend related subjects.