Drawing Book - HackerRank.

NOTE: If you are copying my code then its an advice to you to copy it after downloading it to avoid any kind of compilation error its link is available at the bottom of the source code.



Brie’s Drawing teacher asks her class to open their books to a page number. Brie can either start turning pages from the front of the book or from the back of the book. She always turns pages one at a time. When she opens the book, page  is always on the right side:
image
When she flips page , she sees 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 she wants to turn to page , what is the minimum number of pages she will turn? She can start at the beginning or the end of the book.
Given  and , find and print the minimum number of pages Brie must turn in order to arrive at page .
Function Description
Complete the pageCount function in the editor below. It should return the minimum number of pages Brie must turn.
pageCount has the following parameter(s):
  • n: the number of pages in the book
  • p: the page number to turn to
Input Format
The first line contains an integer , the number of pages in the book.
The second line contains an integer, , the page that Brie's teacher wants her to turn to.
Constraints
Output Format
Print an integer denoting the minimum number of pages Brie must turn to get to page .
Sample Input 0
6
2
Sample Output 0
1
Explanation 0
If Brie starts turning from page , she only needs to turn  page:
Untitled Diagram(6).png
If Brie starts turning from page , she needs to turn  pages:
Untitled Diagram(3).png
Because we want to print the minumum number of page turns, we print  as our answer.
Sample Input 1
5
4
Sample Output 1
0
Explanation 1
If Brie starts turning from page , she needs to turn  pages:
Untitled Diagram(4).png
If Brie starts turning from page , she doesn't need to turn any pages:
Untitled Diagram(5).png
Because we want to print the minimum number of page turns, we print  as our answer.



SOURCE CODE




import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;

public class Solution {

/*
* Complete the pageCount function below.
*/
static int pageCount(int n, int p) {
/*
* Write your code here.
*/
int x1=p/2;
int x2=n/2-p/2;
if(x1<x2)
return(x1);
else
return(x2);
}

private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])*");

int p = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])*");

int result = pageCount(n, p);

bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();

bufferedWriter.close();

scanner.close();
}
}



Click here to Download



OUTPUT








If you have any question then leave a comment below I will do my best to answer that question.

Comments

Popular posts from this blog

[Question 3] ISC 2017 Computer Practical Paper Solved – Caesar Cipher.

Pattern of your own name.

Designing Patterns - Print 'Z'.