• + 0 comments

    The problem sounds tough at first but if you think about it, it's a simple division problem. Here's an easy C# solution -

    	public static int pageCount(int n, int p)
        {
            // When iterating from 1 take the celing value
            double pageTurnNeededFromStart = Math.Ceiling((double)(p-1)/2);
            
            // when iterating from end - take the ceiling value if the end page is even, otherwise take floor value
            double pageTurnNeededFromEnd = n%2==0? Math.Ceiling((double)(n-p)/2) : Math.Floor((double)(n-p)/2);
            
            return (int)Math.Min(pageTurnNeededFromStart, pageTurnNeededFromEnd);
        }