Drawing Book HackerRank Problem Solution

In this Drawing Book HackerRank Problem, 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  is always on the right side:

Problem Solution in Java

public static int pageCount(int n, int p)
    {
        if(n%2 == 0){
            n++;
        }
        var pageFlipFromStart = Math.Floor((double)p/2);
        var pageFlipFromEnd = Math.Floor((double)(n-p)/2);
        return pageFlipFromStart < pageFlipFromEnd ? (int)pageFlipFromStart : (int)pageFlipFromEnd;
    }
Solve original Problem on HackerRank here. Checkout more HackerRank Problems

Leave a Comment