How To Solve HackerRank Drawing Book Problem

How To Solve HackerRank Drawing Book Problem


How To Solve HackerRank Drawing Book Problem

In this video, I have explained hackerrank drawing book solution algorithm.
hackerrank drawing book problem can be solved by using mathematics. The complexity of drawing book hackerrank solution is O (1). This hackerrank problem is a part of Practice | Algorithms | Implementation | Drawing Book hackerrank challenge.

For simplicity, I have divided this hackerrank tutorial into 3 parts.
[00:00]Understanding the problem statement.
[03:24] Building the logic to solve the problem.
[08:48] 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 HackerRank Drawing Book …  

âśš 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 #HackerRankSolutions #HackerRankTutorials #implementation #drawingbook #HackerRank #JavaAidTutorials #Programming #DataStructures #algorithms #coding #competitiveprogramming #JavaAidTutorials #Java #codinginterview #problemsolving #KanahaiyaGupta #hackerrankchallenges


Content

6.72 -> Hey everyone, I am Kanahaiya Gupta and welcome to my YouTube channel.
11.2 -> today I am going to show you how to solve drawing book hackerrank problem but before
18.029 -> moving ahead make sure you have subscribed to my YouTube channel.
22.53 -> Let's start.
24.43 -> In this problem Brie’s drawing Teacher asks her class open their books for page number
32.4 -> Brie can either start turning pages from the front of the book or from the back of the
40.25 -> book she always turns pages one at a time when she opens the book page one is always
48.51 -> on the right side.
49.51 -> so, you can see page one is always right side.
54.26 -> When she flips to page 1, she sees page 2 and 3. each page except the last page will always
63.83 -> be printed on both sides, like a book we all know right.
69.25 -> the last page may only be printed on the front given the length of the book. If the book is
78.29 -> n pages long and she wants to turn to page p, what is the minimum number of pages
85.58 -> she will turn? she can start at the beginning or the end of the book. so the main question
92.13 -> here guys, Given n and p find and print the number of pages, Brie must turn in order to
100.54 -> arrive at page p. so we have a book and there are total n pages and we have to reach on
107.96 -> pth page which is present at the pth location so what is the minimum number of
112.34 -> turn we have to use to reach to that location either from the back side or either from the
118.299 -> front side whatever is minimum we have to return as an answer.so in input we have the
127.99 -> first line contains an integer n the number of pages present in the book and the second
133.86 -> line contains an integer p, the Brie’ teacher wants her to turn to.
140.23 -> so here we have 6, total number of 6 pages and we want to move to a 2nd page. so you can
147.48 -> see if we go from the front side it required only one turn to reach to the 2nd page and
155.76 -> we go from the back side it requires 2 turns which one is minimum first one,1.
162.28 -> so the answer will be 1.
167.73 -> Similar in 2nd example we have all total 5 pages and we have to reach to the 4th page
175.849 -> so when we turn the page from the front side it requires 2 turns to reach to the 4th page
182.56 -> but when we go from the back side there is no turn required it's all already there so
189.04 -> we required minimum, what is the minimum value this which is 0.
193.48 -> so, we require 0 number of terms.
196.27 -> so, this is a problem guy, now let's build the logic around it.
202.99 -> so, I have taken one example to show you how it works.
210.349 -> so, I have a book with total 11 pages n= 11 and I want to reach toward the 4th page, target
220.22 -> page is 4th. so, there are two ways to reach 4th either I can go from the front side or
228.4 -> I can go from the back side.
230.72 -> So, let’s see both the things, so if I go from the front side so how many turns are
238.73 -> required?
239.73 -> 1,2 so total two turns are required then I will reach to my target page.
248.54 -> if I go from the back side then how many turns are required?1,2 3 so total 3 turns are required
262.84 -> and we have to return the minimum number of turns so what is the minimum number of turns
269.13 -> here.
270.99 -> it will be 2 so 2 would be our answer but how we are going to achieve this programmatically
282.33 -> in this problem one thing is for sure the first page will always start from right of
287.51 -> the page and book first page always start from the right-hand side but from the left
293.59 -> when we talk about the back side there is no guarantee
296.66 -> when the page will end it may and here also and it made here also but we are sure whenever
303.41 -> the books start, the first page will always be on a right side and we know each and every
310.71 -> page contains two pages.
312.28 -> so, every page is having 2 pages.
315.47 -> to reach to the last page or total number of pages from left to right we require 11
321.39 -> total number of the page by 2. because on every page we have 2 pages.
327.71 -> which would be 5 .it means from starting to reach up to end we require 5 turns to reach
336.46 -> up to 11th page.
337.85 -> you can see here 1,2,3,4,5 so we require 5 turns to reach to the 11th page.
354.139 -> now many numbers of terms are required to reach the target page which is p. if you are
359.88 -> going from left to right it requires 4 by 2 which equals to 2.
365.35 -> so that 2 turns are required to reach up to 4. you can see here so this is 1 and this
374.66 -> is 2.
376.199 -> so, once you turn the page 2 times, you will reach to 4 which is our target page.so this is what
384.259 -> we have concluded from front side to go up to the end we require TLR means from left
390.509 -> to right total number of pages required 5 and we are going to the target location total
396.229 -> number of pages required is 2.
399.42 -> now we have to calculate from backside how many times or how many turns it will take
404.15 -> to reach to the same location.
406.6 -> we can say from back side means from right to left how many numbers of turns are required?
413.23 -> the total number of turns is required from left to right - turns required to reach the
420.389 -> target location from left to right.
422.259 -> so, we know if we are going from front, we required total by 2 or we can see n by 2 n
430.59 -> is here the number of pages - how many turns you require from front PLR is p by 2 whatever
441.41 -> will get its number of turns required from the back side and finally we have to return
447.85 -> minimum value from there.
449.949 -> so, if we put this value in this formula what we will get 5 - 2 = 3 so what is the minimum
459.62 -> of 2 and 3 is 2. so answer will be 2.
467.9 -> I hope guys this is clear to you what we have done here we are calculating how many turns
474.4 -> are required from the front side to reach a particular target location and how many
481.27 -> turns are required to reach up to the end from the front side so you want to calculate
485.971 -> the same thing from the back side so we are just subtracting that from the total and why
492.75 -> we have taken the front side not the back side because front side we are sure ,
496.87 -> it always starts from 1 in but backside we are not sure where to it will end so that's
502.45 -> why we have taken or considering front as our base and calculating the total number
509.89 -> of turns required to reach to end and then we are subtracting the total number of turns
515.5 -> required to reach up to the target location from the front side to get the location from
521.7 -> the back side.
524.93 -> let's see the algorithm in action guys.
526.72 -> So, this is the method guys which is given in a hackerrank temperate and we are accepting
535.129 -> 2 parameters n which is the total number of pages and p is a target page where we have
542.679 -> to reach in the minimum number of turns.
545.69 -> So total page turn count from front side, how many pages we have to turn from the front
551.899 -> side to reach to the total number of pages or the final last page is n by 2 and a target
560.47 -> page turn count from the front side to reach to the target page, how many turn we have to
567.009 -> make which is p by 2 and the same way target page count from the back side to reach to
575.739 -> the target page how many pages we have to turn from the back side equal to total page
581.339 -> turn count from the front minus target page count from the front so we'll get some number
588.92 -> here and then finally, we have to return the minimum number of turns required to reach
596.73 -> to that page. so we are just extracting the minimum and returning. so let's copy this code
605.91 -> and try to run on a hackerrank platform.
617.48 -> sample test case got passed. now I am submitting the solution.
625.04 -> congratulations guys,
626.309 -> You have done a great job. I hope this problem is clear to you.
630.139 -> please let me know if there are some doubts.
633.72 -> if you want the source code you can download the same from my GitHub repository and the
637.769 -> link is mentioned in the description box.
640.94 -> thanks for watching guys

Source: https://www.youtube.com/watch?v=aM2Y2PeXrhk