Sort by

recency

|

227 Discussions

|

  • + 0 comments

    The maximum possible result is k − 1. If there exist two numbers ≤ n that both contain all the 1-bits of k − 1, then the answer is k − 1; otherwise it must be k − 2.

    This can be checked in O(1) by testing whether (k − 1) | k ≤ n.

    `typescript

    function getMaxLessThanK(n, k)
    {
        return ((k | (k - 1)) <= n) ? k - 1 : k - 2;
    }
    

    `

  • + 0 comments

    function getMaxLessThanK(n, k) { let max = 0; for (let a = 1; a <= n; a++) { for (let b = a + 1; b <= n; b++) { if((a & b) < k) max = Math.max(a & b, max);
    }
    } return max; }

  • + 0 comments

    This was my answer

    function getMaxLessThanK(n, k) {
        let max = 0;
        for (let i = 1; i < n; i++)
            for(let j = i + 1; j <= n; j++)
                if (max < (i & j) && (i & j) < k) {
                    max = (i & j);
                    if (max === k - 1) return max; // since it's the most expected value
                } 
        return max;
    }
    
  • + 0 comments

    function getMaxLessThanK(n,k){ let maxValue = 0; for(let i = 1; i

            let binaryAndValue = i & j;
            if(binaryAndValue >= k){
                break;
            }
            if(binaryAndValue > maxValue){
                maxValue = binaryAndValue;
            }
    
        }
    }
    return maxValue;
    

    }

  • + 0 comments
    function getMaxLessThanK(n, k) {
      let max = 0;
      let s = [];
    
    
        max = 0;
        for (let a = 1; a <= n; a++) {
          s.push(a);
          for (let b = a + 1; b <= n; b++) {
            let bit = a & b;
            if (bit < k && bit > max) {
              max = bit;
            }
          }
        }
      return max;
    }