Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank, Algorithm Solutions of Problem Solving Section in Java. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for Drawing Book in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Introduction To Algorithm
The word Algorithm means “a process or set of rules to be followed in calculations or other problem-solving operations”. Therefore Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed upon in order to get the expected results.
Advantages of Algorithms:
- It is easy to understand.
- Algorithm is a step-wise representation of a solution to a given problem.
- In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program.
Link for the Problem – Drawing Book – Hacker Rank Solution
Drawing Book – Hacker Rank Solution
Problem:
A teacher asks the class to open their books to a page number. A student can either start turning pages from the front of the book or from the back of the book. They always turn pages one at a time. When they open the book, page is always on the right side:
![Drawing Book in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 2 image](https://s3.amazonaws.com/hr-challenge-images/0/1481920803-d2b54f38f0-book.png)
When they flip page , they see pages and . Each page except the last page will always be printed on both sides. The last page may only be printed on the front, given the length of the book. If the book is pages long, and a student wants to turn to page , what is the minimum number of pages to turn? They can start at the beginning or the end of the book.
Given and , find and print the minimum number of pages that must be turned in order to arrive at page .
Example
![Drawing Book in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 3 Untitled Diagram(4).png](https://s3.amazonaws.com/hr-challenge-images/22564/1467398281-32b69f6fa9-UntitledDiagram4.png)
Using the diagram above, if the student wants to get to page , they open the book to page , flip page and they are on the correct page. If they open the book to the last page, page , they turn page and are at the correct page. Return .
Function Description
Complete the pageCount function in the editor below.
pageCount has the following parameter(s):
- int n: the number of pages in the book
- int p: the page number to turn to
Returns
- int: the minimum number of pages to turn
Input Format
The first line contains an integer , the number of pages in the book.
The second line contains an integer, , the page to turn to.
Constraints
Sample Input 0
6 2
Sample Output 0
1
Explanation 0
If the student starts turning from page , they only need to turn page:
![Drawing Book in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 4 Untitled Diagram(6).png](https://s3.amazonaws.com/hr-challenge-images/22564/1467398713-1decf68d06-UntitledDiagram6.png)
If a student starts turning from page , they need to turn pages:
![Drawing Book in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 5 Untitled Diagram(3).png](https://s3.amazonaws.com/hr-challenge-images/22564/1467397150-52d0a8213b-UntitledDiagram3.png)
Return the minimum value, .
Sample Input 1
5 4
Sample Output 1
0
Explanation 1
If the student starts turning from page , they need to turn pages:
![Drawing Book in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 3 Untitled Diagram(4).png](https://s3.amazonaws.com/hr-challenge-images/22564/1467398281-32b69f6fa9-UntitledDiagram4.png)
If they start turning from page , they do not need to turn any pages:
![Drawing Book in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 7 Untitled Diagram(5).png](https://s3.amazonaws.com/hr-challenge-images/22564/1467398392-5d9ac72e45-UntitledDiagram5.png)
Return the minimum value, 0.
Drawing Book – Hacker Rank Solution
import java.util.Scanner; /** * @author Techno-RJ * */ public class DrawingBook { static int pageCount(int n, int p) { int totalPageTurnCountFromFront = n / 2; int targetPageTurnCountFromFront = p / 2; int targetPageTurnCountFromBack = totalPageTurnCountFromFront - targetPageTurnCountFromFront; return Math.min(targetPageTurnCountFromFront, targetPageTurnCountFromBack); } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int p = in.nextInt(); System.out.println(pageCount(n, p)); in.close(); } }
This article is really helpful in understanding the problem solving approach for the Drawing Book HackerRank problem. It was very clearly explained with the help of example Java code snippets. The diagrams included in the article were also very helpful in understanding the concept. Great work!
buy tadalafil 20mg sale tadalafil 40mg sale sexual dysfunction
order vermox pill buy vermox 100mg sale tadalis 10mg tablet
order avana 200mg sale buy diclofenac 50mg generic purchase cambia sale
buy generic indocin 50mg buy terbinafine generic order generic cefixime 100mg
purchase catapres generic order spiriva 9mcg generic buy spiriva 9mcg online cheap
minocin price pioglitazone online actos 30mg drug
buy leflunomide paypal viagra cheap buy azulfidine 500mg
buy absorica amoxil oral order generic azithromycin
azithromycin 250mg canada cheap neurontin 600mg neurontin pills
buy furosemide online buy lasix generic order generic ventolin inhalator
buy levitra 10mg sale order vardenafil pill buy plaquenil pill
order altace 5mg glimepiride us buy etoricoxib generic
buy levitra 20mg without prescription levitra 20mg without prescription order plaquenil 200mg online cheap