Sort by

recency

|

2059 Discussions

|

  • + 0 comments

    Just the do mathematics & it's simple (Code in C# )-

                public static int pageCount(int n, int p)
                        {
                                int shortestRoute = 0;
                                if(n/2 < p){
                                        //Start from back side
                                        if(n%2 != 0){
                                                shortestRoute = (int)(n-p)/2;    
                                        }else{
                                                shortestRoute = (int)((n-p)+1)/2;
                                        }   
                                }
                                else{
                                        //Start from biggining
                                        shortestRoute = (int)p/2;
                                }
    
                                return shortestRoute;
                        }
    
  • + 0 comments
    def pageCount(n, p):
        # Write your code here
        if p==1 or p==n:
            return 0
        else:
            if p%2==0:
                k=p
            else:
                k=(p-1)
            return min(k//2, (n-k)//2)
    
  • + 0 comments
    function pageCount(n, p) {
        if(p == 1 || p == n){
            return 0
        }
        let forwardTrunCount = parseInt(p/2);
        let backwardTrunCount = parseInt((n/2)-forwardTrunCount)
        return Math.min(forwardTrunCount,backwardTrunCount)
    
    }
    
  • + 0 comments
        public static int pageCount(int n, int p) {
        // Write your code here
        if(p==n || p==1 || p==0)
            return 0;
        return Math.min((p/2),(n/2)-(p/2));
    
        }
    
  • + 0 comments

    public static int pageCount(int n, int p) {

        int rightDist = p/2;
    
        int leftDist = (n-p+ (p%2 == 0 ? 0 : 1) ) / 2;
    
        return Math.min(rightDist, leftDist);
    
    }