Drawing Book

Sort by

recency

|

134 Discussions

|

  • + 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)
    
  • + 0 comments

    My solution in Python

    def pageCount(n, p):
        if p % 2 == 0 and n == p + 1:
            return 0
        
        if n % 2 == 0:
            n += 1
        
        return min(int(p/2), int((n - p)/2))