Sort by

recency

|

2092 Discussions

|

  • + 0 comments

    My humble python solution:

    # Write your code here
    
    #left to right
    min_left = p // 2
    
    #right to left
    n = n + 1 if n % 2 == 0 else n
    min_right = (n - p) // 2
    
    return min_left if min_left <= min_right else min_right
    
  • + 0 comments

    Typescript Solution:

    const pageCount = (n: number, p: number): number => Math.min(Math.floor(p / 2), Math.floor(n / 2) - Math.floor(p / 2));
    
  • + 0 comments
    def pageCount(n, p):
        # Write your code here
        start = p // 2
        end = (n - p) // 2
        return start if start < end else end
    
  • + 0 comments

    rust in O(1) solution

    fn page_count(n: i32, p: i32) -> i32 {
        (p / 2).min((n - p + (n + 1) % 2) / 2)
    }
    
  • + 0 comments

    My C# solution; public static int pageCount(int n, int p) { var books = Enumerable.Range(0, n + 1).Chunk(2); int counter = 0; List counters = [];

      foreach(var pages in books)
      {
        if(pages.Contains(p))
          break;
        counter++;
      }
      counters.Add(counter);
      counter = 0;
      books = books.Reverse();
      foreach(var pages in books)
      {
        if(pages.Contains(p))
          break;
        counter++;
      }
      counters.Add(counter);
    
      return Enumerable.Min(counters);
    }