Drawing Book

Drawing Book

Brie’s Drawing teacher asks her class to open their books to a page number. Brie can either start turning pages from the front of the book or from the back of the book. She always turns pages one at a time. When she opens the book, page 1 is always on the right side:

When she flips page 1, she sees 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 she wants to turn to page p, what is the minimum number of pages she will turn? She can start at the beginning or the end of the book.

Given n and p, find and print the minimum number of pages Brie must turn in order to arrive at page p.

Function Description

Complete the pageCount function in the editor below. It should return the minimum number of pages Brie must turn.

pageCount has the following parameter(s):

  • n: the number of pages in the book
  • p: the page number to turn to

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 that Brie’s teacher wants her to turn to.

Constraints

  • 1 <= n <= 105
  • 1 <= p <= n

Output Format

Print an integer denoting the minimum number of pages Brie must turn to get to page p.

Sample Input 0

1
2
6
2

Sample Output 0

1
1

Explanation 0

If Brie starts turning from page 1, she only needs to turn 1 page:

If Brie starts turning from page 6, she needs to turn 2 pages:

Because we want to print the minumum number of page turns, we print 1 as our answer.

Sample Input 1

1
2
5
4

Sample Output 1

1
0

Explanation 1

If Brie starts turning from page 1, she needs to turn 2 pages:

If Brie starts turning from page 5, she doesn’t need to turn any pages:

Because we want to print the minimum number of page turns, we print 0 as our answer.


Solution

1
2
3
4
5
6
7
8
9
10
11
12
/*
* Complete the pageCount function below.
*/
function pageCount(n, p) {
/*
* Write your code here.
*/
let fromLeft = Math.floor(p / 2);
let fromRight = Math.floor(n / 2) - fromLeft;

return fromLeft > fromRight ? fromRight : fromLeft;
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×