Тёмный

HackerRank Solution: Number Line Jumps in C++ (Kangaroo problem) 

nexTRIE
Подписаться 7 тыс.
Просмотров 7 тыс.
50% 1

Опубликовано:

 

27 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 35   
@imangurung6030
@imangurung6030 3 года назад
wat if v2 - v1 is 0? when i put custom input as 70 2 43 2 the answer came YES even tho logically thinking they should never meet but surprisingly when the input is 43 2 70 2 the answer is right which is NO it would be great if u could explain y is that
@nextrie
@nextrie 3 года назад
Excellent remark, Iman, and that is a mistake I have just fixed (see the video's updated description). The explanation for YES with the input "70 2 43 2" initially is that when x1 - x2 gets evaluated, it returns 27, and 27 divided by zero (because v2 - v1 equals zero in this case) evaluates to an infinity value (or INF). So, when we check if the infinity value is equal to the floor of itself (TRUE) and if the infinity value is greater than or equal to 0 (also TRUE), our condition evaluates to TRUE and so we return the string "YES". But when you change the custom input to "43 2 70 2", then this time, x1 - x2 gives us -27. That's a negative value, and when you divided -27 by zero, you get a negative infinity value. So here, when we check if that negative infinity value is equal to the floor of itself (TRUE) and if that negative infinity value is greater than or equal to zero (FALSE), then our condition evaluates to FALSE, and so we execute the last return statement instead, which returns "NO". But this was a flaw in my logic, which I had overlooked, so I have now updated my code and posted it as a public file on GitHub (also in the description of this video now). Here is the link: gist.github.com/IsaacAsante/d2a6eb21c1a4c6757930fd6c8ce5143e You will notice that the condition in the function now uses the isinf() function in C++ to verify if the "res" value is an infinity value or not. If it is, our condition will automatically evaluate to FALSE, and so the answer will be "NO". Hope it's clear to you now, and thanks a lot for pointing out that mistake which was not covered by the test cases on HackerRank.
@imangurung6030
@imangurung6030 3 года назад
@@nextrie Thank you
@nextrie
@nextrie 3 года назад
@@imangurung6030 You're welcome. By the way, I pinned your comment and gave you a shout out in the description.
@LNDS209615
@LNDS209615 3 года назад
@@nextrie I think there is still a problem with the updated code. I am not that capable in writing code in c++ but for example, if you check the case 0 2 0 2 then the kangaroos will meet after the first jump. Still abs(v2 - v1) = 0 but you have to return "YES". Am I right??? I used one more if statement to account for this.
@itiremiroglu7646
@itiremiroglu7646 3 года назад
Hello @nexTRIE, Why you don’t solved Between Two Sets problem? Because I like your solutions & I don’t understand that problem. Keep up the good work!
@nextrie
@nextrie 3 года назад
Hi Itir, I'll definitely solve that problem at some point in the future. Glad you appreciate my videos!
@boomboom-9451
@boomboom-9451 4 года назад
Have you thought about creating a discord group for Competitive Programming, Coding Challenges or IT stuff in general? I think you should. I often follow your videos, it would be great to be in a group with people interested in Coding Challenges in which we can support each others, think about it !
@nextrie
@nextrie 4 года назад
Hey, thanks for the suggestion! I might create a Discord group in the future indeed, so look out. Thanks a lot for following my content. It means a lot.
@boomboom-9451
@boomboom-9451 4 года назад
I'm sure there is, just don't give up I know your channel isn't that big (yet) but you're indeed helping people to understand and learn useful stuff and I personally thank you for that. I hope that discord channel to come to us as soon as possible !
@nextrie
@nextrie 4 года назад
I'll keep pushing and making sure I help as much as I can. I've got more videos coming soon about trees, graphs and every other HackerRank topic. And yes, I'll announce once I start growing my community with Discord and other platforms. Thanks! :)
@anaskaratela7171
@anaskaratela7171 3 года назад
Hey mate can u suggest another approach of this problem using while loop, eg: while(x1>x2 || v1>v2) like i tried this one but thrown me a "Run Time Error"
@nextrie
@nextrie 3 года назад
Hi Anas, you don't have to use an iteration-based approach (e.g. a loop) to solve this challenge. Otherwise, this will negatively affect the time complexity of your solution and may unnecessarily extend the execution time of your program. Feel free to play around with the solution from my GitHub: github.com/IsaacAsante/HackerRank/blob/main/Problem%20Solving/Algorithms/Implementation/Number%20Line%20Jumps/number%20line%20jumps.cpp
@AnkitPandey-es7wk
@AnkitPandey-es7wk 2 года назад
Can you please explain why you use double () double res=double ()
@nextrie
@nextrie 2 года назад
Hey Ankit, this is because I am performing a division. Whenever you need to store the result of a division operation in a program, it's always good practice to declare the variable (used for the result of the division) as a double. For example, 10 / 3 or 24 / 7... these divisions give us floating point numbers which are best stored in variables declared as float or double (depending on the maximum value you expect).
@AnkitPandey-es7wk
@AnkitPandey-es7wk 2 года назад
@@nextrie yes sir but double is datatype like int a=4; but here why we are using function like double () double res=double () a/b
@nextrie
@nextrie 2 года назад
@@AnkitPandey-es7wk I understand your question now. What I'm doing here is called "casting". I'm forcefully casting the result of the division into a double, and storing it in my double-type variable called res. The reason for that is because I'm passing my res variable to the floor() function on the next line, as part of my algorithm. But the floor() function in C++ expects its argument to be of type double, and not int. Now, imagine that the value of the division in the previous line was an integer, e.g. 21 / 3 = 7. In this case, I want the integer value 7 to be converted into the floating point value 7.0, so that I can store it in my res variable of type double. This way, I can pass my res variable (now with the double-type value 7.0) to the floor() function afterward without causing any error. Does that answer your question?
@AnkitPandey-es7wk
@AnkitPandey-es7wk 2 года назад
@@nextrie ok sir thank your for such a great explanation 🙏 🙏
@nextrie
@nextrie 2 года назад
@@AnkitPandey-es7wk You're welcome, Ankit. Hope you subscribe for more tutorials.
@anukritisaxena1887
@anukritisaxena1887 3 года назад
please make a video on the Power Sum problem
@nextrie
@nextrie 3 года назад
Alright!
@hamaed19
@hamaed19 2 года назад
👍👍👍
@chuthi244
@chuthi244 2 года назад
thank you .
@moe42937
@moe42937 4 года назад
Thank you
@nextrie
@nextrie 4 года назад
Thank you too for watching, Mohammed! Are you practicing for a technical interview?
@moe42937
@moe42937 4 года назад
@@nextrie actually i learn those things because first for technical interview and for computer science as part of algorithm and Problem solving
@nextrie
@nextrie 4 года назад
@@moe42937 nice!
@dipeshjha3528
@dipeshjha3528 4 года назад
Nice one
@dipeshjha3528
@dipeshjha3528 4 года назад
I hv solved it already
@nextrie
@nextrie 4 года назад
Nice, did you also use linear equation?
@dipeshjha3528
@dipeshjha3528 4 года назад
@@nextrie I dnt remember bro
@dipeshjha3528
@dipeshjha3528 4 года назад
Yes I too
@nextrie
@nextrie 4 года назад
Cool! By the way, I've got some Trees and Graph algorithm solutions on the way...
@dipeshjha3528
@dipeshjha3528 4 года назад
@@nextrie good bro I m waiting for it
Далее
HackerRank C++ Solution: Diagonal Difference
6:18
Просмотров 7 тыс.
Бокс - Финты Дмитрия Бивола
00:31
Microsoft Makes Windows Worse With AI
9:34
Просмотров 166 тыс.
AI's 'Existential Threat' to Humans
13:26
Просмотров 32 тыс.
Subarray division
33:17
Просмотров 2,8 тыс.
Бокс - Финты Дмитрия Бивола
00:31