• + 0 comments

    java(8)

    public static int pageCount(int n, int p) {
        // Write your code here
    
        int firstStart = 0;
        int lastStart = 0;
        for (int i = 0; i < n; i++) {
    
            firstStart++;
            if (i == p) {
                break;
            }
    
        }
        for (int i = n; i >= 0; i--) {
    
            lastStart++;
            if (i == p) {
                break;
            }
    
        }
        int f;
        int l;
        if (n % 2 == 0) {
            f = (firstStart / 2);
            l = (lastStart / 2);
        } else {
            f = (firstStart / 2);
            l = (lastStart / 2) - 1;
        }
    
        return Math.min(f, l);
    
    }