Coding Problem Keys
Turning Book Pages
Problem Statement
A book can be turned either from front or from the back. When we open a book always we have the page 1 always on the right. The book pages must always be turned one by one instead of multiple pages at once. Each page has two sides, front and back. Hence the last page may have only front side depending on the total number of pages N in the book (If N is even, it will have both sides printed else if N is odd only the front side will be printed).
Now Manoj wants to navigate to a page P in the book by turning the least minimum pages either from front or back. Please help Manoj by completing the program as per the given requirement.
Boundary Condition(S)
1 <= N <= 10000
1 <= P <= N
Input Format
The first line will contain the value of N which represents the total number of pages in the book.
The second line will contain the value of P which represents the page to be navigated.
Output Format
The first line will contain the integer value which is the least minimum pages to be turned either from front or back.
Example Input/Output 1
Input
8
6
Output
1
Explanation
From front, after turn 1, pages 2 & 3 are visible. After turn 2, pages 4 & 5 are visible. In the third turn pages 6 & 7 are visible. So 3 turns are required from front.
From back, the last page back side is page 8. So after turn 1, pages 6 & 7 are visible. So 1 turn is required from the back.
The minimum of 3 and 1 (which is 1) is printed as the output.
Example Input/Output 2
Input
12
4
Output
2
Explanation
From front, after turn 1, pages 2 & 3 are visible. After turn 2, pages 4 & 5 are visible. So 2 turns are required from front.
From back, the last page from back side is page 12. So after turn 1, pages 10 & 11 are visible. After turn 2, pages 8 & 9 are visible. After turn 3, pages 6 & 7 are visible. Only after turn 4, pages 4 and 5 are visible. So 4 turns are required from the back.
The minimum of 2 and 4 (which is 2) is printed as the output.
Hidden Test Cases
Test Case 1
Input
8
5
Output
2
Test Case 2
Input
13
12
Output
0
Test Case 3
Input
354
353
Output
1
Test Case 4
Input
745
562
Output
91
Test Case 5
Input
1576
1
Output
0
Note: Max Execution Time Limit: 5000 millisecs
Solution
Programming Language: Python 3 Language
total=int(input())
navigate=int(input())
if(total%2==0):
total+=1
front=navigate//2
back=(total-navigate)//2
print(min(front,back))
# Published By PKJCODERS
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments