Sort by

recency

|

2060 Discussions

|

  • + 0 comments

    Be aware of rounding issues when dividing odd numbers.

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/1T650YeAwRI

    int pageCount(int n, int p) {
        return min(p/2, n/2-p/2);
    }
    
  • + 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)
    
    }