Counting the number of page turns to arrive at some specified page Here is a HackerRank challenge.  Given a book with n pages (with 1 ≤ n ≤ 105), how many page turns are needed to arrive at page p, starting from either the front or the back?  The book is laid out with page 1 on the right.

I have got following solution to the given problem.

import java.util.*;
public class Solution {

    static int solve(int n, int p){
        // Complete this function
        if(n==p)
            return 0;
        if(p%2==0){
            if(n-p==p)
                return (p-1)/2;
            else{
                if(n-p&gt;p)
                    return p/2;
                else
                    return (n-p)/2;
            }
        }
        if(n%2==0 &amp;&amp; p%2!=0){
            if(n-p==p)
                return (p-1)/2;
            else{
                if(n-p&gt;p)
                    return p/2;
                else
                    return (n-p+2)/2;
                }
        }
        else

             if(n-p&gt;p)
                    return p/2;
                else
                    return (n-p)/2;        

    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int p = in.nextInt();
        int result = solve(n, p);
        System.out.println(result);
    }
}


Although it is working code, I am not satisfied with it. It seems very messy. How can I reduce branch statements in my code, or otherwise improve it?
Math.min(p / 2, n / 2 - p / 2)

Explanation:


If we go from start page \$1\$ takes \$0\$; page \$2,3\$ takes \$1\$; page \$4,5\$ takes \$2\$ and so on. So for \$2k,2k+1\$ we require \$k\$ turns. Hence \$p=2k,2k+1\implies \lfloor p/2\rfloor = k\$
If we go from the back  it depends on:


If \$n\$ is odd then \$n,n-1\$ take \$0\$; \$n-2,n-3\$ takes \$1\$ and so on. So \$n-2k,n-(2k+1)\$ takes \$k\$. 

Let \$n=2q+1\$ then \$p=2q-2k+1,2q-2k\implies \lfloor p/2\rfloor = q-k\$ 

i.e. \$k=q-\lfloor p/2\rfloor=\lfloor n/2\rfloor -\lfloor p/2\rfloor \$
If \$n\$ is even then \$n\$ takes \$0\$; \$n-1,n-2\$ takes \$1\$ and so on. So \$n-(2k-1),n-2k\$ takes \$k\$.

Let \$n=2q\$ then \$p=2q-2k,2q-2k+1\implies \lfloor p/2\rfloor=q-k\$

i.e. \$k=q-\lfloor p/2\rfloor=\lfloor n/2\rfloor -\lfloor p/2\rfloor\$