prepbook
  • Introduction
  • Some common stuff
    • python __repr__
    • HackerRank input tips
  • Data Structures and Algorithms
    • Breadth first search
    • Depth First Search
    • Dijkstra
    • A* Search Algorithm
    • Binary Search
    • python counter
    • Sorting
      • Merge Sort
      • Quick Sort
    • Priority Queue
  • Multiprocessing vs Threading
  • Common Coding
    • Find loop in lin list
    • Maximum sum subarray
  • Coding
    • Valid palindrome
    • Palindrome number
    • Remove duplicates from sorted array
    • Island perimeter
    • Serialize and Deserialize Binary Tree
    • Valid Soduku
    • Word Pattern
    • Word Pattern II
    • Group Anagrams
    • Implement Trie
    • Deep copy list with random node
    • Palindrome Permutation
    • Combination Sum
    • Clone Graph
    • Generate parenthesis
    • Fibonacci Number
    • LRU Cache
    • Merge two sorted arrays in place
    • Hamming Distance
    • Merge K sorted arrays
    • Kth smalles element in BST
    • Kth largest element in an array
    • Remove duplicates from sorted list
    • Power of 2
    • Nested list weight sum
    • SIngle number in a list
    • Factor combinations
    • Delete node from BST
  • hacker Rank
    • Coding
      • print staircase
      • Drawing book
      • Challenge 0
      • Min-Max sum
  • WorkRelatedCoding
    • Rectangle Overlap
  • Python tips
Powered by GitBook
On this page

Was this helpful?

  1. hacker Rank
  2. Coding

Drawing book

Previousprint staircaseNextChallenge 0

Last updated 5 years ago

Was this helpful?

Brie’s Drawing teacher asks her class to open their n-page book to page number p. Brie can either start turning pages from the front of the book (i.e., page number 1) or from the back of the book (i.e., page number n), and she always turns pages one-by-one (as opposed to skipping through multiple pages at once). When she opens the book, page 1 is always on the right side:

image

Each page in the book has two sides, front and back, except for the last page which may only have a front side depending on the total number of pages of the book

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

The first line contains an integer, n', denoting the number of pages in the book.

The second line contains an integer, 'p', denoting the page that Brie's teacher wants her to turn to.

#!/bin/python

import sys


n = int(raw_input().strip())
p = int(raw_input().strip())

# your code goes here
# some boundary conditions
if p == 1 or p == n or (n % 2 != 0 and p == (n - 1)):
    print 0 # no turning is necessary

else:
    fwd_turns = 0
    bwd_turns = 0
    if p % 2 == 0: # even page
        fwd_turns = p//2
        bwd_turns = (n - p) // 2

    else: # odd page number
        fwd_turns = p//2
        if n % 2 == 0:
            bwd_turns = ((n - p) // 2) + 1
        else:
            bwd_turns = (n - p) // 2

    if fwd_turns <= bwd_turns:
        print fwd_turns
    else:
        print bwd_turns