Java and Python Solution for Drawing Book HackerRank Problem
Problem Description :
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 1 is always on the right side:
When they flip page 1, they see pages 2 and 3. 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 n pages long, and a student wants to turn to page p, what is the minimum number of pages to turn? They can start at the beginning or the end of the book.Given n and p, find and print the minimum number of pages that must be turned in order to arrive at page p.
Example 1 :
n = 5
p = 3
ans = 1
If the student wants to get to page 3, they open the book to page 1, flip 1 page and they are on the correct page. If they open the book to the last page, page 5, they turn 1 page and are at the correct page. Return 1.
Example 2 :
n = 6
p = 2
ans = 1
If the student starts turning from page 1, they only need to turn 1 page. So return minimum value 1.
Example 3 :
n = 5
p = 4
ans = 0
If the student starts turning from page 1, they need to turn 2 pages. If they start turning from page 5 they do not need to turn any pages.
We have to return number of total pages we turn to get given page p in given all pages n.
Solution 1 : Drawing Book Solution in Java
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
public static int pageCount(int n, int p) {
int ans=0;
// from where to start turning page (start or end)
if (n-p >= p-0) {
ans = (p/2);
} else if (n-1 == p && p%2 != 0) {
ans = 1;
} else {
ans = (n-p) / 2;
}
return ans;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int n = Integer.parseInt(bufferedReader.readLine().trim());
int p = Integer.parseInt(bufferedReader.readLine().trim());
int result = Result.pageCount(n, p);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
Code explanation :
- Here we are checking condition for where to start turn page. from start or end of book (n)
- If given page p is near to 0, we start from 0 otherwise end of given n.
- We have two page number printed on left and right sight so if we device page p by 2 we will get ans.
- If we start from end of book,
- First check if given page is n-p is 1 and p is odd number. If so return 1 directly.
- Otherwise subtract p from n and divide by 2 will get our answer.
Code Output :
n = 6
p = 5
- n-p >= p-0 | 6-5 >= 5-0 becomes false
- Goes to else if condition
- n-1 == p && p%2 != 0 | 6-1 == 5 && 5%2 != 0 becomes true
- Return 1
Lets see another simple code in Java
Solution 2 : Drawing Book Solution in Java
public static int pageCount(int n, int p) {
int frontCount = p/2;
int backCount = n/2-p/2;
return Math.min(frontCount, backCount);
}
We also can do 1 liner using above code
Solution 3 : Drawing Book Solution in One line Java
public static int pageCount(int n, int p) {
return Math.min(p/2 , n/2 - p/2);
}
Solution 4 : Drawing Book Solution in Python
def pageCount(n, p):
if p <= (n - p):
return p
elif n % 2 == 0 and (n - p) == 1:
return 1
else:
return (n - p)
RECOMMENDED ARTICLES :
- Caesar Cipher Example in Java with Explanation
- Recursive Digit Sum HackerRank solution in Java with Explanation
- Find unique elements from given Array in Java with Explanation
- Prime Checker HackeRank Solution in Java with Explanation
Comments
Post a Comment