Sort by

recency

|

424 Discussions

|

  • + 0 comments

    Java 8

    public static int bitwiseAnd(int N, int K) {
        // Write your code here
            int ret=0;
            for(int A=1;A<N;A++)
            {
                for(int B=A+1;B<=N;B++)
                {
                    if((A&B)<K)
                    {
                        ret=Math.max(ret, A&B);
                    }
                }
    
        }
        return ret;
    }
    
  • + 0 comments

    one liner preventing exceeding time limit:

    def bitwiseAnd(N, K):
        return max((a+1)&(K-1) for a in range(N) if K-1 != a+1)
    
  • + 0 comments

    JS

    function bitwiseAnd(N, K) {
        var max = 0;
        for (var i = 1; i <= N; i++) {
            for (var j = i + 1; j <= N; j++) {
                var t = i & j;
                if (t > max && t < K) {
                    max = t;
                }
            }
        }
        return max;
    }
    
  • + 0 comments

    Python 3

    def bitwiseAnd(N, K):
        # Write your code here
        tot = 0
        for j in range(N):
            if (j+1) != (K-1):
                ba = (K-1)&(j+1)
                tot = max(ba, tot)
        return tot
    
  • + 0 comments
    def bitwiseAnd(N, K):
        
        max_A_and_B = 0
        
        for A in range(1, N):
            for B in range(A+1, N+1):
                A_and_B = A & B
                if (A_and_B > max_A_and_B) and (A_and_B < K):
                    max_A_and_B = A_and_B
    
        return max_A_and_B