# Drawing Book HackerRank Solution in C, C++, Java, Python

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

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. Return1 .

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<=10^5
• 1<=p<=n

Sample Input 0

```6

2```

Sample Output 0

`1`

## Drawing book HackerRank 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 HackerRank Solution in C++

```#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);
return 0;
}```

## Drawing book HackerRank 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();
int ans = Math.min(p/2,(n-p)/2);
System.out.println(ans);
}
}```

## Drawing book HackerRank Solution in Python

```#!/bin/python

import sys

n = int(raw_input().strip())
p = int(raw_input().strip())
print min(p/2,(n-p)/2)

Drawing book HackerRank Solution in C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {

static void Main(String[] args) {