Drawing Book

Sort by

recency

|

135 Discussions

|

  • + 0 comments

    Great explanation of the page-turning problem! For more interesting and easy-to-follow recipes like this, check out Recipe Lookbook.

  • + 0 comments

    Java

    public static int pageCount(int n, int p) {
            // Write your code here
            
            int currentLeaf = 0;
            int countLeaf = 0;
            int minimum = 0;
            
            for(int page = currentLeaf; page <= n; page++){
                if(currentLeaf == p || currentLeaf + 1 == p) break;
                currentLeaf+=2;
                countLeaf++;
            }
            
            minimum = countLeaf;
            
            currentLeaf = n % 2 == 0 ? n+1 : n;
            countLeaf = 0;
            for(int page = currentLeaf; page > 0; page--){
                if(currentLeaf == p || currentLeaf - 1 == p) break;
                currentLeaf-=2;
                countLeaf++;
            }
            
            return minimum > countLeaf ? countLeaf : minimum;
        }
    
  • + 0 comments

    def pageCount(n, p): # Write your code here return min(p//2,(n//2)-(p//2))

  • + 0 comments

    one liner solution in python return int(min(p-0,(n+1 if n%2==0 else n)-p)/2)

  • + 0 comments
    #python solution
    def pageCount(n, p):
        total = math.floor(n/2)
        goal = math.floor(p/2)
        
        return min(goal,total-goal)