# Minimum number of page turns to get to a desired page

Given a book of **N** pages, the task is to calculate the minimum number of page turns to get to a give desired page **K**. We can either start turning pages from the front side of the book (i.e from page 1) or from the back side of the book (i.e page number N). Each page has two sides, front and back, except the first page, which has only back side and the last page which may only have back side depending on the number of pages of the book.

Examples :

Input :N = 6 and K = 2.Output :1. From front, (1) -> (2, 3), page turned = 1. From back, (6) -> (4, 5) -> (2,3), page turned = 2. So, Minimum number of page turned = 1.Input :N = 5 and K = 4.Output :1. From front, (1) -> (2, 3) -> (4,5), page turned = 2. From back, (4, 5) page turned = 1. From back, it is 2nd page, since 4 is on other side of page 5 and page 5 is the first one from back So, Minimum number of page turned = 1.

The idea is to calculate distance of the desired page from the front and from the back of the book, minimum of this is the required answer.

Now, Consider there is page 0, which is front of the first page. And if N is even, consider there is page N+1, which is back of the last page, so total number of pages are N+1.

To calculate the distance,

1. If K is even, front distance = (K – 0)/2 and back distance = (N – 1 – K)/2.

2. If K is odd, front distance = (K – 1)/2 and back distance = (N – K)/2.

## C++

`// C++ program to find minimum number of page ` `// turns to reach a page ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `minTurn(` `int` `n, ` `int` `k) ` `{ ` ` ` `// Considering back of last page. ` ` ` `if` `(n%2 == 0) ` ` ` `n++; ` ` ` ` ` `// Calculating Distance from front and ` ` ` `// back of the book and return the min ` ` ` `return` `min((k + 1)/2, (n - k + 1)/2); ` `} ` ` ` `// Driven Program ` `int` `main() ` `{ ` ` ` `int` `n = 6, k = 2; ` ` ` `cout << minTurn(n,k) << endl; ` ` ` `return` `0; ` `} ` ` ` `// This code is modified by naveenkonda ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find minimum ` `// number of page turns to ` `// reach a page ` `import` `java.io.*; ` ` ` `public` `class` `GFG ` `{ ` ` ` `// Function to calculate ` `// minimum number of page ` `// turns required ` `static` `int` `minTurn(` `int` `n, ` `int` `k) ` `{ ` ` ` ` ` `// Considering back of last page. ` ` ` `if` `(n % ` `2` `== ` `0` `) ` ` ` `n++; ` ` ` ` ` `// Calculating Distance from front and ` ` ` `// back of the book and return the min ` ` ` `Math.min((k + ` `1` `) / ` `2` `, (n - k + ` `1` `) / ` `2` `); ` `} ` ` ` ` ` `// Driver Code ` ` ` `static` `public` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `6` `, k = ` `2` `; ` ` ` `System.out.println(minTurn(n, k)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find minimum number ` `# of page turns to reach a page ` `def` `minTurn(n, k): ` ` ` ` ` `# Considering back of last page. ` ` ` `if` `(n ` `%` `2` `=` `=` `0` `): ` ` ` `n ` `+` `=` `1` ` ` ` ` `/` `/` `Calculating Distance ` `from` `front ` `and` ` ` `/` `/` `back of the book ` `and` `return` `the ` `min` ` ` `return` `min` `((k ` `+` `1` `) ` `/` `2` `, (n ` `-` `k ` `+` `1` `) ` `/` `2` `) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `6` ` ` `k ` `=` `2` ` ` `print` `(` `int` `(minTurn(n, k))) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find minimum ` `// number of page turns to ` `// reach a page ` `using` `System; ` ` ` `public` `class` `GFG ` `{ ` ` ` `// Function to calculate ` `// minimum number of page ` `// turns required ` `static` `int` `minTurn(` `int` `n, ` `int` `k) ` `{ ` ` ` ` ` `// Considering back of last page. ` ` ` `if` `(n % 2 == 0) ` ` ` `n++; ` ` ` ` ` `// Calculating Distance from front and ` ` ` `// back of the book and return the min ` ` ` `return` `Math.Min((k + 1) / 2, ` ` ` `(n - k + 1) / 2); ` `} ` ` ` ` ` `// Driver Code ` ` ` `static` `public` `void` `Main (String[] args) ` ` ` `{ ` ` ` `int` `n = 6, k = 2; ` ` ` `Console.WriteLine(minTurn(n, k)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find minimum number ` `// of page turns to reach a page ` ` ` `function` `minTurn(` `$n` `, ` `$k` `) ` `{ ` ` ` `// Considering back of last page. ` ` ` `if` `(` `$n` `% 2 == 0) ` ` ` `$n` `++; ` ` ` ` ` `// Calculating Distance from front and ` ` ` `// back of the book and return the min ` ` ` `return` `min((` `$k` `+ 1) / 2, ` ` ` `(` `$n` `- ` `$k` `+ 1) / 2); ` `} ` ` ` `// Driver Code ` `$n` `= 6; ` `$k` `= 2; ` `echo` `minTurn(` `$n` `, ` `$k` `) ; ` ` ` `// This code is contributed by nitin mittal. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

1

**Time Complexity :** O(1)

This article is contributed by **Anuj Chauhan(anuj0503)**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Find a number which give minimum sum when XOR with every number of array of integers
- Smallest number dividing minimum number of elements in the array | Set 2
- Smallest number dividing minimum number of elements in the Array
- Minimum number of cubes whose sum equals to given number N
- Minimum number of items to be delivered
- Minimum sum obtained by choosing N number from given N pairs
- Minimum number of changes required to make the given array an AP
- Minimum number of stacks possible using boxes of given capacities
- Minimum prime number operations to convert A to B
- Make n using 1s and 2s with minimum number of terms multiple of k
- Minimum removals in a number to be divisible by 10 power raised to K
- Find minimum number of steps to reach the end of String
- Minimum number of moves to make all elements equal
- Minimum number of additons to make the string balanced
- Minimum number of integers required to fill the NxM grid
- Minimum number of operations on an array to make all elements 0
- Minimum number of moves after which there exists a 3X3 coloured square
- Minimum value of K such that sum of cubes of first K natural number is greater than equal to N
- Minimum number of coins that can generate all the values in the given range
- Minimum number of operations to convert a given sequence into a Geometric Progression