Sort by

recency

|

2078 Discussions

|

  • + 0 comments

    Here is problem solution in Python, Java, C++, C and Javascript - https://programmingoneonone.com/hackerrank-drawing-book-problem-solution.html

  • + 0 comments

    python solution: I imagined a fictive page at the start (page 0), and, if the n was even, I also imagined a fictive page at the end (increased the value of n).

    def pageCount(n, p):
        if n % 2 == 0:
            n += 1
            
        left = p // 2
        right = (n - p) // 2
            
        return min(left, right)
    
  • + 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

    Constant time solution C++

    int pageCount(int n, int p) {
        int ffront;
        int fback;
        if(n%2 == 0){
            fback = (int)ceil((n-p)/2.0);
        }
        else{
            fback = (int)floor((n-p)/2.0);
        }
    
    ffront = (int)ceil((p-1)/2.0);
    return min(ffront, fback);
    

    }

  • + 0 comments

    * Constant time solution C++

    • int pageCount(int n, int p) {
    • int ffront;
    • int fback;
    • if(n%2 == 0){
    • fback = (int)ceil((n-p)/2.0);
    • }
    • else{
    • fback = (int)floor((n-p)/2.0);
    • }
    • ffront = (int)ceil((p-1)/2.0); return min(ffront, fback); }