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.
public static int pageCount(int n, int p) {
/*
If the last page is Even, It will be on left or right side of the page,
But to reach the page p from first page, if we divide it by 2 , we will
get the number of pages we have to turn as every page contains two pages.
/
int firstpagePath = p/2;
/
From the last page if we divide number of pages n by 2, we will get
how many pages, we have to turn to get to the last page.
So, Basically it will take n/2-p/2 pages to get to the page p from the
last page
*/
int lastpagePath = n/2-(p/2);
/*
Ultimately we are returning the least number of pages,we have to turn
to get to the page p
*/
return Math.min(firstpagePath,lastpagePath);
}
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 →
public static int pageCount(int n, int p) { /* If the last page is Even, It will be on left or right side of the page, But to reach the page p from first page, if we divide it by 2 , we will get the number of pages we have to turn as every page contains two pages. / int firstpagePath = p/2; / From the last page if we divide number of pages n by 2, we will get how many pages, we have to turn to get to the last page. So, Basically it will take n/2-p/2 pages to get to the page p from the last page