Drawing Book

  • + 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);
    }