We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Drawing Book
  5. Discussions

Drawing Book

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 1899 Discussions, By:

recency

Please Login in order to post a comment

  • a0970606353
    17 hours ago+ 0 comments
    def pageCount(n, p):
        # Write your code here
        if p%2==0:
            start = 1+(p-2)//2
            end = (n-p)//2
        if p%2==1:
            start = (p-1)//2
            end = (n+1-p)//2
        
        return min(start,end)
    
    0|
    Permalink
  • joaquinagv99
    2 days ago+ 0 comments

    JavaScript :

    function pageCount(n, p) {
        // Write your code here
        let min = 1;
        let max = n % 2 == 0 ? n : n - 1
        
        for(let i = 0; i <= n ; i++){
            if(min >= p || max <= p){
                return i
            }
            min += 2;
            max -= 2;
        }
    
    }
    
    1|
    Permalink
  • m_d_mushtaq0309
    3 days ago+ 0 comments
    def pageCount(n, p):
        # Write your code here
        if p == 1 or n==p:
            return '0'
        else:
            if (n-p)< p:
                if n-p <=1 and p %2 !=0:
                    return '1'
                else:
                    return (n-p)//2
            else:
                return p//2
    
    0|
    Permalink
  • shinejayakumar
    5 days ago+ 0 comments
    def pageCount(n, p):
       
        is_odd = n % 2 != 0
        if p > n/2:
            if is_odd and n-p <= 1:
                return 0
            return math.ceil((n-p-1)/2) if is_odd else math.ceil((n-p)/2)
        return math.ceil((p-1)/2)
    
    0|
    Permalink
  • ainsleyCrawford
    7 days ago+ 0 comments

    C#

        public static int pageCount(int n, int p)
        {
          if (n % 2 == 1) n--;
          int l2R = 0, r2L = 0;
          for (int i = 1; i < p; i += 2) l2R++;
          for (int i = n; i > p; i -= 2) r2L++;
          return Math.Min(l2R, r2L);
        }
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy