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

Flipping the Matrix HackerRank Solution in Java with Explanation

Java Solution for Flipping the Matrix | Find Highest Sum of Upper-Left Quadrant of Matrix Problem Description : Sean invented a game involving a 2n * 2n matrix where each cell of the matrix contains an integer. He can reverse any of its rows or columns any number of times. The goal of the game is to maximize the sum of the elements in the n *n submatrix located in the upper-left quadrant of the matrix. Given the initial configurations for q matrices, help Sean reverse the rows and columns of each matrix in the best possible way so that the sum of the elements in the matrix's upper-left quadrant is maximal.  Input : matrix = [[1, 2], [3, 4]] Output : 4 Input : matrix = [[112, 42, 83, 119], [56, 125, 56, 49], [15, 78, 101, 43], [62, 98, 114, 108]] Output : 119 + 114 + 56 + 125 = 414 Full Problem Description : Flipping the Matrix Problem Description   Here we can find solution using following pattern, So simply we have to find Max of same number of box like (1,1,1,1). And ...

Sales by Match HackerRank Solution | Java Solution

HackerRank Sales by Match problem solution in Java   Problem Description : Alex works at a clothing store. There is a large pile of socks that must be paired by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are. For example, there are n=7 socks with colors socks = [1,2,1,2,1,3,2]. There is one pair of color 1 and one of color 2 . There are three odd socks left, one of each color. The number of pairs is 2 .   Example 1 : Input : n = 6 arr = [1, 2, 3, 4, 5, 6] Output : 0 Explanation : We have 6 socks with all different colors, So print 0. Example 2 : Input : n = 10 arr = [1, 2, 3, 4, 1, 4, 2, 7, 9, 9] Output : 4 Explanation : We have 10 socks. There is pair of color 1, 2, 4 and 9, So print 4. This problem easily solved by HashMap . Store all pair of socks one by one in Map and check if any pair is present in Map or not. If pair is present then increment ans variable by 1 ...