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.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Drawing Book
  5. Discussions

Drawing Book

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 1523 Discussions, By:

votes

Please Login in order to post a comment

  • lemming0061
    5 years ago+ 87 comments

    Extremely simple solution in Python

    #!/bin/python
    
    import sys
    
    
    n = int(raw_input().strip())
    p = int(raw_input().strip())
    
    print min(p/2,n/2-p/2)
    
    271|
    Permalink
    View more Comments..
  • Kanahaiya
    3 years ago+ 10 comments

    Hello friends,

    In this video tutorial, I have explained hackerrank drawing book solution algorithm. hackerrank drawing book problem can be solved by using mathematics. The complexity of drawing book hackerrank solution is O (1)

    If interested to know more about the generic algorithm in details-

    click here for the video explanation of generic algorithm with complexity analysis.

    or you can click on the image too to follow youtube tutorial.

    Here is the working solution:-

    source code :

    static int pageCount(int n, int p) {
    
    		int totalPageTurnCountFromFront = n / 2;
    		int targetPageTurnCountFromFront = p / 2;
    		int targetPageTurnCountFromBack = totalPageTurnCountFromFront - targetPageTurnCountFromFront;
    
    		return Math.min(targetPageTurnCountFromFront, targetPageTurnCountFromBack);
    
    	}
    

    Would really appreciate your feedback like, dislike , comment etc. on my video.

    Do not forget to upvote, if you find it useful.

    186|
    Permalink
    View more Comments..
  • Aswath3167
    5 years ago+ 24 comments

    My solution in C...Hope it helps!!!

    #include<stdio.h>
    int main()
    {
    int n=0,p=0,min;
    scanf("%d",&n);
    scanf("%d",&p);
    min=(n/2)-(p/2);
    if(min>p/2)
    min=p/2;
    printf("%d",min);
    return 0;
    }
    
    75|
    Permalink
    View more Comments..
  • sakai_stanley
    3 years ago+ 10 comments

    My JavaScript solution:

    function pageCount(n, p) {
        /*
    		  n: the number of pages in the book
    		  p: the page number to turn to
        */
    
        const pageTurns = Math.floor(p / 2);
        const totalTurns = Math.floor(n / 2);
    
        /* Returns the total number of page turns it takes to get
        to a page or how many it requires if starting from the back */
    
        return Math.min(pageTurns, totalTurns - pageTurns);
    }
    
    73|
    Permalink
    View more Comments..
  • aruncveli
    5 years ago+ 2 comments

    Input for test case #26 has to be on two lines.

    20|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature