Hello Programmers, In this post, you will know how to solve the HackerRank Drawing Book Solution. This problem is a part of the HackerRank Algorithms Series.
One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.
HackerRank Drawing Book Solution
Task
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 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 n, 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
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 1 page and are at the correct page. Return 1.
Function Description
Complete the pageCount 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.
Constraints
- 1 <= n <= 105
- 1 <= p <= n
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 Solution
Drawing Book Solution in C
#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); int ans1,ans2; ans1=p/2; ans2=(n-p)/2; if(ans1>ans2) { printf("%d",ans2); } else { printf("%d",ans1); } return 0; }
Drawing Book Solution in Cpp
#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <string> #include <bitset> #include <cstdio> #include <limits> #include <vector> #include <climits> #include <cstring> #include <cstdlib> #include <fstream> #include <numeric> #include <sstream> #include <iostream> #include <algorithm> #include <unordered_map> using namespace std; int main(){ int n; cin >> n; int p; cin >> p; cout<<min(p/2,(n-p)/2); // your code goes here return 0; }
Drawing Book Solution in Java
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int p = in.nextInt(); // your code goes here int mid = (n + 1) / 2; if(p < mid) { System.out.println(p / 2); } else { System.out.println((n - p) / 2); } } }
Drawing Book Solution in Python
#!/bin/python3 import sys n = int(input().strip()) p = int(input().strip()) # your code goes here print(min(p//2,n//2-(p//2)))
Drawing Book Solution using JavaScript
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() { const n = parseInt(readLine()); const p = parseInt(readLine()); // your code goes here const n_prime = n % 2 === 0? n - 1: n; let from_front = Math.floor(p / 2); let from_back = Math.floor((n_prime - p) / 2); if (n % 2 === 0) { from_back++; } from_front < from_back? console.log(from_front): console.log(from_back); }
Drawing Book Solution in Scala
object Solution { def main(args: Array[String]) { val sc = new java.util.Scanner (System.in) val n = sc.nextInt val p = sc.nextInt val t = (n + 2) / 2 val c = (p + 2) / 2 print((c - 1) min (t - c)) } }
Drawing Book Solution in Pascal
uses math; CONST fi='test.INP'; fo='test.OUT'; maxn=1000000; var n,p:longint; function dau(t:longint):longint; var i,j,dem:longint; begin j:=t; dem:=0; while j<>1 do begin if (j mod 2 = 0) then begin j:=j-1; dem:=dem+1; end else j:=j-1; end; exit(dem); end; function cuoi(t:longint):longint; var i,j:longint; begin j:=n; i:=0; while j<>t do begin if j mod 2 = 0 then begin dec(j); inc(i); end else begin j:=j-1; end; end; exit(i); end; PROCEDURE nhap; var i,j,t:longint; begin readln(n,p); t:=min(dau(p),cuoi(p)); writeln(t); end; BEGIN nhap; END.
Disclaimer: This problem (Drawing Book) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.