Hello coders, today we are going to solve **Drawing Book HackerRank Solution** which is a Part of **HackerRank Algorithm** Series.

Contents

**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Â **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Â

**, what is the minimum number of pages to turn? They can start at the beginning or the end of the book.**

*p*GivenÂ ** n**Â andÂ

**, find and print the minimum number of pages that must be turned in order to arrive at pageÂ**

*p***.**

*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,Â

*, the page to turn to.*

**p****Constraints**

**1 <=***n*<= 10^{5}**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**.

**Solution – Drawing Book**

**C++**

#include <vector> #include <iostream> #include <algorithm> static int total_pages(const int& n) { return (n + 2) / 2; } int main() { int n,p; std::cin >> n >> p; int entire_book = total_pages(n); int from_front = total_pages(p) - 1; int from_back = entire_book - from_front - 1; std::cout << std::min(from_front, from_back) << '\n'; return 0; }

**Python**

import sys n = int(input().strip()) p = int(input().strip()) print(min(p//2,n//2-(p//2)))

**Disclaimer: **The above Problem **(Drawing Book)** is generated by **Hacker Rank **but the Solution is Provided by **CodingBroz.** This tutorial is only for** Educational** and **Learning** Purpose.