Skip to main content

Drawing Book HackerRank Solution in Java and Python with Explanation

Java and Python Solution for Drawing Book HackerRank Problem

Drawing Book HackerRank Solution in Java and Python with Explanation

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:

Page 1 example
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 :

  1. Caesar Cipher Example in Java with Explanation
  2. Recursive Digit Sum HackerRank solution in Java with Explanation
  3. Find unique elements from given Array in Java with Explanation
  4. Prime Checker HackeRank Solution in Java with Explanation  


Comments

Popular posts from this blog

Queen's Attack II HackerRank Solution in Java with Explanation

Queen's Attack II Problem's Solution in Java (Chessboard Problem)   Problem Description : You will be given a square chess board with one queen and a number of obstacles placed on it. Determine how many squares the queen can attack.  A queen is standing on an n * n chessboard. The chess board's rows are numbered from 1 to n, going from bottom to top. Its columns are numbered from 1 to n, going from left to right. Each square is referenced by a tuple, (r, c), describing the row r and column c, where the square is located. The queen is standing at position (r_q, c_q). In a single move, queen can attack any square in any of the eight directions The queen can move: Horizontally (left, right) Vertically (up, down) Diagonally (four directions: up-left, up-right, down-left, down-right) The queen can move any number of squares in any of these directions, but it cannot move through obstacles. Input Format : n : The size of the chessboard ( n x n ). k : The number of obstacles...

Java Hashset HackerRank Solution | Programming Blog

Java Hashset HackerRank Solution with Explanation   Problem Statement :- In computer science, a set is an abstract data type that can store certain values, without any particular order, and no repeated values. {1,2,3} is an example of a set, but {1,2,2} is not a set. Today you will learn how to use sets in java by solving this problem. You are given n pairs of strings. Two pairs (a,b) and (c,d) are identical if a = c and b = d. That also implies (a,b) is not same as (b,a). After taking each pair as input, you need to print number of unique pairs you currently have. See full problem description in HackerRank Website :- https://www.hackerrank.com/challenges/java-hashset/problem Let's see solution of problem. import java.util.HashSet; import java.util.Scanner; public class Solution {     public static void main(String[] args) {         Scanner s = new Scanner(System.in);         System.out.println("Enter tot...