We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
The problem sounds tough at first but if you think about it, it's a simple division problem. Here's an easy C# solution -
publicstaticintpageCount(intn,intp){// When iterating from 1 take the celing valuedoublepageTurnNeededFromStart=Math.Ceiling((double)(p-1)/2);// when iterating from end - take the ceiling value if the end page is even, otherwise take floor valuedoublepageTurnNeededFromEnd=n%2==0?Math.Ceiling((double)(n-p)/2):Math.Floor((double)(n-p)/2);return(int)Math.Min(pageTurnNeededFromStart,pageTurnNeededFromEnd);}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Drawing Book
You are viewing a single comment's thread. Return to all 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 -