Sort by

recency

|

2052 Discussions

|

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/1T650YeAwRI

    int pageCount(int n, int p) {
        return min(p/2, n/2-p/2);
    }
    
  • + 0 comments

    i made solution using python!

    def pageCount(n, p):
        front = p // 2
        back = (n - p) // 2 if n % 2 != 0 else (n - p + 1) // 2
        return min(front, back)
    
  • + 0 comments

    in python I use 2 loop

    def pageCount(n, p):
        nbFlipFront = 0
        pageFront = 1
        pageBack = n
        nbFlipBack = 0
        while pageFront != p+1:
            print(f"actual page from front:{pageFront}")
            if pageFront%2 == 0:
                nbFlipFront += 1
            pageFront+=1
        
        while pageBack != p :
            print(f"actual page from back:{pageBack}")
            if pageBack%2 == 0:
                nbFlipBack +=1
            pageBack -=1
    
  • + 0 comments

    This question is flawed, for one of the test cases they assert that n = 5 and p = 4 should yield 0 pages flipped. But a book with 5 pages has an unused or blank page as the 6th page. If one were to start from the back of the book, having only flipped the back cover and no pages, they would be looking at the 6th page. To view the 4th page would require 1 flip, exposing pages 4 and 5. Thus n = 5 and p = 4 should yield 1?

    "Each page except the last page will always be printed on both sides. The last page may only be printed on the front, given the length of the book." - This is an example where the last page, page 5 is printed on the front, given the length of the book, n = 5.

  • + 0 comments

    int pageCount(int n, int p) { return min(n / 2 - p / 2, p / 2); }