-5

Hackerrank problem Drawing Book.

Brie’s Drawing teacher asks her class to open their books to a page number. Brie can either start turning pages from the front of the book or from the back of the book. She always turns pages one at a time. When she opens the book, page 1 is always on the right side:

enter image description here

When she flips to page 1, she sees pages 2 and 3. Each page except the last page will always be printed on both sides. The last page may only be printed on the front, given the length of the book. If the book is n pages long, and she wants to turn to page p, what is the minimum number of pages she will turn? She can start at the beginning or the end of the book.

Given n and p, find and print the minimum number of pages Brie must turn in order to arrive at page p.

Function Description

Complete the pageCount function in the editor below. It should return the minimum number of pages Brie must turn.

pageCount has the following parameter(s):

  • n: the number of pages in the book
  • p: the page number to turn to

Input Format

The first line contains an integer n, the number of pages in the book. The second line contains an integer, p, the page that Brie's teacher wants her to turn to.

Constraints

enter image description here

Output Format

Print an integer denoting the minimum number of pages Brie must turn to get to page p.

Sample Input 0

6
2

Sample Output 0

1

enter image description here

Sample Input 1

5
4

Sample Output 1

0

===================================================

8 Answers 8

3

One should realize that 2 pages form a unit, using integer division by 2.

You did not utilize the remainder dropping integer division, where 12 / 7 == 1.

static int pagesTurnCount(int n, int p) {
    n /= 2; // >>= 1
    p /= 2;
    return Math.min(p, n - p); // From front, from back
}

       p
 0  >  1  <  2  <  3      page pairs
- 1   2 3   4 5   6 -     pages

The zero based programmer should be aware that that last index for double-pages is indeed "the number of pages" n divided by 2.

0
2

Each time a page is turned 2 more pages get visible. So, it means that in the worst case the teacher needs n/2 turns to get from the first page to the last one. However, since she's also allowed to turn from the end of the book, she can do the worst case in n/4 flips.

So, if her page number is p <= n/2, then she needs p/2 flips, otherwise - n/2 - p/2 steps or just min(p/2, (n/2) - (p/2)):

uint32_t pageCount(uint32_t n, uint32_t p) {
    return std::min(p / 2, (n/2) - (p/2));
}

EDIT

Since a solution in JAVA is required I'm adding it here:

int pageCount(int n, int p) {
    return Math.min(p / 2, (n/2) - (p/2));
}
2

Javascript solution:

function pageCount(n, p) {
    var fromFront = Math.floor(p/2);
    var fromBack = (n%2 ===0 && p%2 !==0)? Math.floor((n-p)/2)+1 : Math.floor((n-p)/2);
    return fromFront>fromBack?fromBack:fromFront;
}
1
def pageCount(n, p):
    if p==n or p==1:
        c=0
    elif n%2!=0 and p==n-1:
        c=0
    else:
        if p%2==0:
            if n%2==0:
                c=min(p/2,(n-p)/2)
            else:
                c=min(p/2,(n-p-1)/2)
        else:
            if n%2==0:
                c=min((p-1)/2,(n-p+1)/2)
            else:
                c=min((p-1)/2,(n-p)/2)
return int(c)
1

Here's my solution to that

fun pageCount(n: Int, p: Int): Int {
    val pages = IntArray(n) { it }
    val pairs = mutableListOf<Pair<Int, Int>>()
    for (i in 0..n - 1 step 2) {
        val lastItem = if (pages[i] + 1 >= pages.size) 0 else 1
        pairs.add(pages[i] to pages[i + lastItem])
    }
    
    var forwardTurns = 0
    var backwardTurns = 0
    pairs.apply {
        this.forEach { pair ->
            if (pair.first == p || pair.second == p) return@apply else forwardTurns++
        }
    }
    for (i in pairs.size - 1 downTo 0) {
        if (pairs[i].first == p || pairs[i].second == p) break else backwardTurns++
    }
    return Math.min(forwardTurns, backwardTurns)
}
1
import java.io.*;     
import java.util.*;     
public class Solution {      
private static int pageCount(int n, int p) {      
return Math.min(p / 2, (n/2) - (p/2));
}      
public static void main(String[] args){     
Scanner sc= new Scanner(System.in);        
int n=sc.nextInt();        
int p=sc.nextInt();   
System.out.println(pageCount(n,p));
    }
}
 

    
1

Here's my Python3 solution to that

forward = 0
backward = 0
i, i_ = 0, 1

if n % 2 != 0:
    j, j_ = n-1, n
else:
    j, j_ = n, n+1

while p not in (i, i_, j, j_):
    i += 2
    i_ += 2
    forward += 1
    j -= 2
    j_ -= 2
    backward += 1

if backward < forward:
    return backward
else:
    return forward
1

simple python solution

def pageCount(n, p):
    back_val=0
    res=[i for i in range(n+1)]
    new=[res[i:i+2] for i in range(0,len(res),2)]
    
    for i in range(len(new)):
        if p in new[i]:
            front_val=i
        if p in new[::-1][i]:
            back_val=i
            print(back_val) 
                    
    return min([front_val,back_val])

Not the answer you're looking for? Browse other questions tagged or ask your own question.