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.
intpagesInTheBook=n;intpageToReach=p;// We need to bring to the closest odd integer because:// - since we are using ints, the division will always be rounded down// - e.g. 6 pages total and we need to reach page 5 => coming from the end, (6 - 5) / 2 = 1 / 2 => rounds down to 0// when we need ((6+1) - 5) / 2 = 2 / 2 = 1if(pagesInTheBook%2==0)++pagesInTheBook;// Moving from end, we need to only get to the desired number, therefore we exclude the pages from start// until desired page.// Also each 2 values means 1 page flip just like counting from the startintfromBack=(pagesInTheBook-pageToReach)/2;// Moving from start, each 2 values means 1 page flip// Therefore, number of page flips is (desired page / 2)intfromFront=pageToReach/2;returnMath.min(fromBack,fromFront);
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 →
Java with some explanation