HackerRank Drawing Book Problem Solution Yashwant Parihar, April 12, 2023April 13, 2023 In this post, We are going to solve HackerRank Drawing Book 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 1 is always on the right side: When they flip to 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 the page p. Example n = 5 p = 3 Using the diagram above, 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 pages and are on the correct page. Return 1. Function Description Complete the page count 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 n, the number of pages in the book.The second line contains an integer, p, the page to turn to. Sample Input 0 6 2 Sample Output 0 1 Explanation 0 If the student starts turning from page 1, they only need to turn 1 page: If a student starts turning from page 6, they need to turn 2 pages: Return the minimum value, 1. Sample Input 1 5 4 Sample Output 1 0 Explanation 1 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 Return the minimum value, 0. HackerRank Drawing Book Problem Solution Drawing Book C Solution #include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> int main(){ int n; scanf("%d",&n); int p; scanf("%d",&p); // your code goes here int pagesFromFront = (p - (p % 2))/2; int diffFromBack = (n - p); int pagesFromBack; if(n % 2 != 0) { pagesFromBack = (diffFromBack - (diffFromBack % 2))/2; } else { diffFromBack = diffFromBack += 1; pagesFromBack = (diffFromBack - (diffFromBack % 2))/2; } if(pagesFromBack < pagesFromFront) printf("%d",pagesFromBack); else printf("%d",pagesFromFront); return 0; } Drawing Book C++ Solution #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int n, p; cin >> n >> p; int p0 = 1, p1 = p/2+1, p2 = n/2+1; cout << min(p1-p0, p2-p1); return 0; } Drawing Book C Sharp Solution using System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution { static void Main(String[] args) { int n = Convert.ToInt32(Console.ReadLine()); int p = Convert.ToInt32(Console.ReadLine()); // your code goes here Console.WriteLine(Math.Min(p, n - p) / 2); } } Drawing Book Java Solution 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; public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner in = new Scanner(System.in); int N = in.nextInt(); int P = in.nextInt(); if(N < 1 || N > 100000) quit(); if(P < 1 || P > N) quit(); int leftP = 0; int rightP = 0; int currentP = 1; while(currentP < P) { currentP = currentP + 2; leftP++; } currentP = N%2==0 ? N : N-1; while(currentP > P) { if(currentP == P) break; currentP = currentP - 2; rightP++; } System.out.println(leftP > rightP ? rightP : leftP); in.close(); } public static void quit() { System.out.println("0"); System.exit(0); } } Drawing Book JavaScript Solution process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; process.stdin.on('data', function (data) { input_stdin += data; }); process.stdin.on('end', function () { input_stdin_array = input_stdin.split("\n"); main(); }); function readLine() { return input_stdin_array[input_currentline++]; } /////////////// ignore above this line //////////////////// function main() { var n = parseInt(readLine()); var p = parseInt(readLine()); // your code goes here console.log( Math.min(Math.floor(p/2), Math.floor((n - p)/ 2))); /* if(p == 1 || p == n || p == n-1) { console.log(0); } else { var step = 1; reverse = false; var mid = Math.floor(n/2); if(p > mid+1) { reverse = true; } if(!reverse) { var i = 2; while(i != p && i < n-1) { i = i+2; step++; } } else { var i = n - 2; while(i != p && i > mid+1) { i = i - 2; step++; } } console.log(step); }*/ } Drawing Book Python Solution #!/bin/python3 import sys n = int(input().strip()) p = int(input().strip()) if p==1 or p==n or p==n-1: print(0) else: x = 2 pageList = [] while x<n-1: pageList.append([x,x+1]) x+=2 forward = 1 # print(pageList) for i in pageList: if p in i: break forward+=1 # print("forward", forward) if forward <= len(pageList)//2: print(forward) else: print(len(pageList)-forward +1) Other Solutions HackerRank Counting Valleys Problem Solution HackerRank Electronics Shop Problem Solution c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython