100-days-of-rust/Week-06/Day-42_Drawing-Book
2024-09-05 11:19:45 +02:00
..
day42 Wrote program for Day 42 2024-09-05 11:19:45 +02:00
README.md Add files via upload 2023-03-23 21:14:34 -04:00

Drawing Book

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:

page1

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 return the minimum number of pages that must be turned in order to arrive at page p.

Example

Considering n = 5 and p = 3:

minPages(5, 3) ➞ 1

page1

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.