Sort by

recency

|

426 Discussions

|

  • + 0 comments

    JavaScript/TypeScript

    function bitwiseAnd(N: number, K: number): number {
        let max = 0;
        let aANDb;
    
        for (let a = 1; a < N; a++) {
            for (let b = a + 1; b <= N; b++) {
                aANDb = a & b;
                if ((aANDb) < K && (aANDb) > max) {
                    max = aANDb;
                }
            }
        }
    
        return max;
    }
    
  • + 1 comment
    def bitwiseAnd(N, K):
        if (K - 1) | K <= N:
            return K - 1
        return K - 2
    

    the max value before k will be k-1 is it exists print k-1 else k-2 the next largest value before k-1

  • + 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;
    }