Sort by

recency

|

2110 Discussions

|

  • + 0 comments

    Here is Drawing Book solution in Python, Java, C++, C and Javascript programming - https://programmingoneonone.com/hackerrank-drawing-book-problem-solution.html

  • + 0 comments

    Page Turns in Rust: Solving the Book Page Count Problem in O(1)

    fn pageCount(n: i32, p: i32) -> i32 {
        if n/2 < p { //from the end
            return n / 2 - p / 2;
        } else { //from beginning
            return p / 2;
        }
    }
    

    This function computes the minimum number of page turns needed to reach page p in a book of n pages. The student can start turning pages either from the front (page 1) or from the back (page n).

    Key idea:

    Number of page turns from the front: from_front = p/2 Because every page turn advances 2 pages (left + right).

    Number of page turns from the back: from_back=n/2−p/2 Here n/2 is the total number of “spreads” in the book, and p/2 is how many spreads we’ve passed from the front.

    Time complexity: The function performs only a couple of integer operations and a conditional -->O(1) time.

    Space complexity: It uses only fixed scalar variables, no extra data structures --> O(1) space.

  • + 0 comments

    There are two types of people: Type 1:

    #return min((p//2), ((n//2)-(p//2)))
    '
    Type 2:
    '
    listi = []
        for i in range(0, n+1, 2):
            listi.append((i, i+1))
        for i in range(len(listi)):
            if p in listi[i]:
                front = i
        back = len(listi) - front - 1
        return min(front, back)
    
  • + 0 comments

    Simple Python Solution:

    def pageCount(n, p):
        return min( p, n-p +(n%2!=1) )//2
    
  • + 0 comments
    def pageCount(n, p):
        papers=(n//2)+1
        paper=(p//2)+1
        if (papers-paper)<paper:
            return (papers-paper)
        else:
            return paper-1