Тёмный
No video :(

Save the Prisoner HackerRank solution [ Algorithm + Implementation ] 

JAVAAID - Coding Interview Preparation
Подписаться 37 тыс.
Просмотров 31 тыс.
50% 1

Save the prisoner hackerrank problem can be solved easily by using modulo operator. The complexity of hackerrank Save the prisoner solution is O(1) which is a part of Practice | Algorithms | Implementation | Save the prisoner hackerrank challenge.
This video explains how to solve save the prisoner hackerrank problem in java. Here I have explained the algorithm to solve hackerrank save the prisoner problem separately and then implemented the same algorithm using java.
For simplicity, I have divided this hackerrank tutorial into 3 parts.
1) Understanding the problem statement.
2) Building the logic to solve the problem.
3) Coding the logic using java (you can use your own preferred programming language).
🔴 Subscribe for more hackerrank tutorials: / @javaaidtutorials
👍 Like us on Facebook: / hackerranksolutiontuto...
💎Share this video with a RU-vidr friend: • Save the Prisoner Hack...
✚ Join our community ►
👉 Coding interview preparation group: / codingip
👉 Telegram link: t.me/hackerran...
📖 Resources ►
👉 Problem statement: www.hackerrank...
👉 Source code: bit.ly/2T8TlmS
✅ Recommended playlists ►
👉 All hackerrank solutions: • How To Master In Data ...
👋 Let’s Connect ►
Git Hub: github.com/kan...
Twitter: / kanahaiyagupta
Linked in: / kanahaiya-gupta
Facebook: / coolkanahaiya
Instagram: / coolkanahaiya
#JAVAAID #HackerRankSolutions #HackerRankTutorials #implementation #savetheprisoner #HackerRank #JavaAidTutorials #Programming #DataStructures #algorithms #coding #competitiveprogramming #JavaAidTutorials #Java #codinginterview #problemsolving #KanahaiyaGupta #hackerrankchallenges

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

 

22 авг 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 94   
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
Hello Coding Lover, If you have any doubts or any better approach to solve this problem, let me know in comments.
@ratnamalajajee8513
@ratnamalajajee8513 3 года назад
Sir can u explain the code inside the main method?
@wurly99
@wurly99 4 года назад
You can also do it as one line solution with: (m+s-2)%n+1 This way we don't need to check if modulo is 0, since we shift result by 1 before modulo and then add it back.
@JavaAidTutorials
@JavaAidTutorials 4 года назад
yup, i came to know later, after uploading the tutorial..:)
@__blatatat
@__blatatat 4 года назад
How did you arrive at this?
@wurly99
@wurly99 4 года назад
We calculate last number with (m+s-1), then we use modulo n to "wrap" it around our n number of prisoners. But this would mess result if we end up on the last prisoner (if (m+s-1)%n == 0). Therefore we fix it by substracting 1 from the sum before modulo and then adding it back.
@viktorstefanov4848
@viktorstefanov4848 3 года назад
@@wurly99 why the last number is m+s-1 and not just m+s. I don't understand the logic isn't it if s=3 we start counting like 3, 4, 5, 6...n ?
@wurly99
@wurly99 3 года назад
@@viktorstefanov4848if you don't offset with -1 you end up on the next number not last. See for example with 1 sweet (m = 1) and chair 3 (s = 3). If you count as m+s you end up on chair 3+1 = 4 which is not true, with 1 sweet you should end up on the same number you started.
@chauhanashutoshgajanan1564
@chauhanashutoshgajanan1564 4 года назад
This is the best channel whenever you are stuck in any hackerrank problem.
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Thank you. 🙂
@Bobby-mf6fw
@Bobby-mf6fw 4 года назад
I was facing tle ,bt now got the efficient way toh solv this prblm👍
@preetiipriya
@preetiipriya 2 года назад
i came up with another approach but it's exceeding the time limits: int i=s; while(i
@tanson86
@tanson86 4 года назад
I was using the formula (m+s)%n-1 and only 60% of the TC were passing. Your solution helped me identify the bug. Thanks.
@Satelliteua
@Satelliteua 5 лет назад
In the first example, S is 2, but you start distributing from the 1st prisoner. The answer to that should actually be 6.
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
There was a reason why i have started with 1 because counting starts from one so its simple to calculate if starts from one. I you see the complete video,you will come to know. I already informed that currently i am starting with one to make you understand but in this question we have to start from two so answer will be shift by two place. Please let me know, if still you have some doubts?
@travelnlearn
@travelnlearn 2 года назад
Thank You Sir for this awesome and genuine content. This q looked difficult to me before but now its a piece of cake for me. All thanks to you Sir, May GBU with lots of love long life success happiness and enjoyment Thanks a lot again
@kennethz1539
@kennethz1539 4 года назад
Hey JAVVAID, thanks for the insight! But I think in your explanation, there is an error where "s" is actually equal to 2 not 7 at 5 : 49 so( 5 + 2 - 1) % 7 still return 6 which is the correct answer...
@JavaAidTutorials
@JavaAidTutorials 4 года назад
It is a good observation through. :) but if you listen to the video again at 5:28 I said let suppose, I have taken s=7 just to make you understand the use of modulo.. :)
@kakashisenpai99
@kakashisenpai99 4 года назад
Such a great explanation , just coming from another channel who tried copy your style exactly but failed to deliver the concept properly.
@JavaAidTutorials
@JavaAidTutorials 4 года назад
people can easily copy someone's style but its hard to copy the experience.
@PIYUSH-lz1zq
@PIYUSH-lz1zq 2 года назад
Bro , after this playlist...from where can I get same questions like this ?? Real world story based question ??
@arrebcer4906
@arrebcer4906 5 лет назад
Please make more videos on dynamic problems and solutions approach like this.
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
Sure. I already started it. luckily today, I have uploaded a tutorial on DP which explain basic theoretical stuff. go and check right now..:)
@arindamdas235
@arindamdas235 Год назад
please make a series on codechef algorithem
@arrebcer4906
@arrebcer4906 5 лет назад
Nice solution.. I couldn't think this way...
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
thank you. Keep watching..Keep learning..!!
@amanrai8010
@amanrai8010 5 лет назад
How you came to this easy solution I was trying hell lot of things but didnt arrive to the solution. Anyway Thanks for making nice tutorial
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
The same I have explaned in solution:). Thanks for watching..!!
@SubhamKumar-9009
@SubhamKumar-9009 3 года назад
nice solution
@youshinde367
@youshinde367 4 года назад
Nice explaination thank u sir
@claudelounou9925
@claudelounou9925 4 месяца назад
thanks you very much ❤❤
@chandra_yoo6742
@chandra_yoo6742 4 года назад
great question loved your explanation
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Glad you enjoyed it!
@md-ayaz
@md-ayaz 5 лет назад
can you do series on Project Euler questions on Hackerrank? Btw, you are the best!
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
Thanks @Mohammed Ayaz S for your compliment. currently i am focusing on those tutorial which people wants to watch more. as it takes lot of effort and time to create a tutorial so at least i want couple of people to watch it.Lets see , how it goes. if possible then will try this also.
@melk48111
@melk48111 4 года назад
love your videos. Keep going
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Thank you.!!
@sunnysharma5635
@sunnysharma5635 4 года назад
How you comes up with this solution
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Do more practice..!! then you will be able to see the patterns which can be applied.
@aryayunanta1470
@aryayunanta1470 3 года назад
Thanks!
@sachinkumbar5089
@sachinkumbar5089 4 года назад
excellent sir!
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Thank you..!!
@abhinavprasoon218
@abhinavprasoon218 4 года назад
nicely explained...
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Thank you 😃
@eggikh
@eggikh 4 года назад
it helped ! thanks !
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Glad it helped!
@harshkumargupta8538
@harshkumargupta8538 3 года назад
thank you mate literally you are helping me a lot .
@PIYUSH-lz1zq
@PIYUSH-lz1zq 2 года назад
Bro , after this playlist...from where can I get same questions like this ?? Real world story based question ??
@PIYUSH-lz1zq
@PIYUSH-lz1zq 2 года назад
Bro , after this playlist...from where can I get same questions like this ?? Real world story based question ??
@IamDemotivated
@IamDemotivated 4 года назад
thank you sir and upload more video.
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Welcome 😊. Don't worry, will upload more. Mean while, please check All hackerrank solutions playlist on our channel. We have uploaded around 40+ videos on hackerrank.
@Pr3kashSingh
@Pr3kashSingh 2 года назад
Thanks.
@seanwilliams1248
@seanwilliams1248 4 года назад
Genius.
@ADITYAGAIKWAD
@ADITYAGAIKWAD 4 года назад
Can anyone give a shorter code.
@JavaAidTutorials
@JavaAidTutorials 4 года назад
I wish, some one can help you on this.😊
@tarandeepsingh4291
@tarandeepsingh4291 4 года назад
sir , can u tell me why we are doing -1 in (r+s-1)%n?
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Suppose you are standing at a point 1 and then moved to point 3, so how many unit you shifted from initial position. 3 -1=2 unit. -0-------1--------2--------3------------ same thing i have done, to calculate the relative shift, you need to subtract the starting position. I hope it make sense, would suggest watch the video again from 4:31 now.
@fancyAlex1993
@fancyAlex1993 5 лет назад
Thank you GOOD SIR
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
Welcome..:)
@smartcoder1112
@smartcoder1112 5 лет назад
Keep doing the good work bhai..
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
Thanks brother..!!
@uttarakhandtravel2130
@uttarakhandtravel2130 5 лет назад
Friend I have gone with the same approach but I didnot passed the test cases having n=100 plzz help
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
if you can share your code and the error, i can review it , what is the problem?
@uttarakhandtravel2130
@uttarakhandtravel2130 5 лет назад
@@JavaAidTutorials can I share pictures on RU-vid comments
@JavaAidTutorials
@JavaAidTutorials 5 лет назад
i dont thik so. share in text format otherwise how i will run your code?
@shubhsagar6234
@shubhsagar6234 3 года назад
Buddy, but in the question 2 represents the starting seat and you said at 2:04 that 2 represents the no of candy every prisoner get.
@shubhsagar6234
@shubhsagar6234 3 года назад
3:05
@JavaAidTutorials
@JavaAidTutorials 3 года назад
If you divide 19/7 =2. which shows number of candies every prisoner will receive irrespective of from where he start. I would recommend try watching the video again for better understanding.
@rohitagarwal7294
@rohitagarwal7294 4 года назад
why are we doing (r+s-1)%n...what is is the need of %n...without %n it is passing 2 testcases and then it is not passing further..
@JavaAidTutorials
@JavaAidTutorials 4 года назад
candies can be greater than the number of students , if so then you will get integer overflow error that is reason we are taking the modulo with n so that it will never cross the array range.the output of this (r+s-1)%n calculation will always lie between 0 to n which is a valid array index.
@manvirchoudhary107
@manvirchoudhary107 3 года назад
Bro why you stopped creating the videos.
@dineshm5575
@dineshm5575 4 года назад
Sir, I did not get the denominatoron why you have added n as in (r+s-1)%n
@JavaAidTutorials
@JavaAidTutorials 4 года назад
candies can be greater than the number of students , if so then you will get integer overflow error that is reason we are taking the modulo with n so that it will never cross the array range.the output of this (r+s-1)%n calculation will always lie between o to n which is a valid array index.
@Sujoyr
@Sujoyr 4 года назад
How did you came to this algorithm...i was trying with m%n+(s-1) but never thought about %n..... How did you find the bug?
@JavaAidTutorials
@JavaAidTutorials 4 года назад
there is nothing special just remember one rule whenever some problem talk about circular traversing always consider modulo operation, it will never give you side effect 😜
@vaishnaviwagh7227
@vaishnaviwagh7227 4 года назад
s=2,but you have considered in 7 in (r+s-1)
@JavaAidTutorials
@JavaAidTutorials 4 года назад
If you listen my words carefully, you will find i said suppose, just to explain the use of modulo, i have taken some random bigger value s=7 for example.
@m.kaviyarasanshan789
@m.kaviyarasanshan789 4 года назад
Sir,tell me what are the questions important for IBM interview sir please replay
@JavaAidTutorials
@JavaAidTutorials 4 года назад
I have not attended IBM interview, so can not tell what questions are important. Will suggest instead of looking for important questions, learn important concepts that will help you more in cracking the coding Interview.
@yakiowokonomi6032
@yakiowokonomi6032 4 года назад
Hello I'm back again, I can only solve the sample input 0 & 1 but failed other ten cases ,so sad I use similar way but it doesn't work
@JavaAidTutorials
@JavaAidTutorials 4 года назад
Please paste your code here so that people can look into it.
@vikashkumarbaitha3323
@vikashkumarbaitha3323 4 года назад
Algo name ?
@JavaAidTutorials
@JavaAidTutorials 4 года назад
this is not a specific algo, its a use-case of mathematics.
@sharabugnanesh3098
@sharabugnanesh3098 Год назад
#before seeing this video my code is like this 😅 """ def saveThePrisoner(a,b,c): l=list(i for i in range(1,a+1)) x=c-1 y=None for i in range(b): y=l[x] x+=1 if y==l[-1]: x=0 print(y) if __name__ == '__main__': for _ in range(int(input())): a,b,c=list(map(int,input().split())) result = saveThePrisoner(a,b,c) """
Далее
How To Solve HackerRank Drawing Book Problem
11:00
Просмотров 17 тыс.
How To Solve Counting Valleys HackerRank Problem
17:00
Python vs C++ Speed Comparison
1:04
Просмотров 2 млн
Running an SQL Injection Attack - Computerphile
17:11
New Year Chaos HackerRank Solution
23:56
Просмотров 52 тыс.