Sort by

recency

|

2108 Discussions

|

  • + 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
    
  • + 0 comments

    The problem sounds tough at first but if you think about it, it's a simple division problem. Here's an easy C# solution -

    	public static int pageCount(int n, int p)
        {
            // When iterating from 1 take the celing value
            double pageTurnNeededFromStart = Math.Ceiling((double)(p-1)/2);
            
            // when iterating from end - take the ceiling value if the end page is even, otherwise take floor value
            double pageTurnNeededFromEnd = n%2==0? Math.Ceiling((double)(n-p)/2) : Math.Floor((double)(n-p)/2);
            
            return (int)Math.Min(pageTurnNeededFromStart, pageTurnNeededFromEnd);
        }
    
  • + 1 comment
      int pz = n / 2;
          int p2 = p / 2;
          int high = pz - p2;
          int low  = p2 - 0;
    
          if(low < high)
          {
            return low;
    
          }
          else
          {
            return high;
          }