Drawing Book

Sort by

recency

|

215 Discussions

|

  • + 0 comments

    short c++ solution. the main idea is that we can calculate how many pages to turn to get number 'x' by turning 'x//2' pages (divide with rounding down e.g. 5//2 = 2). It's because on every display of 2 pages there are 2 consecutive pages e.g. (2,3), (3,4) etc. All we gotta do is just see if we can get from 0->p by turning p//2 pages or from n->p which is n//2-p//2.

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

    C#

        public static int pageCount(int n, int p)
        {
            var totalPages = n%2 == 0 ? n+1 : n;
            return Math.Min(p/2, ((totalPages - p)/2));
        }
    
  • + 0 comments

    Javascript

    function pageCount(n: number, p: number): number {
        const totalFlips = Math.floor(n/2);
        const myP = Math.floor(p/2);
        return Math.min(myP, totalFlips - myP);
    }
    
  • + 0 comments

    C# SOLUTION

    if (p / 2 <= (n % 2 == 0 ? (n+1-p) / 2 : (n-p) / 2)) { return p/2; } return n % 2 == 0 ? (n+1-p) - 2 : (n-p) / 2;

  • + 0 comments

    Here is - HackerRank Drawing Book problem solution