Sort by

recency

|

223 Discussions

|

  • + 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;
    }
    
  • + 0 comments
     if(n<k)return n-1;
        else if(n==2 || n==1 || n==0)
       return 0;
        return k-1;
    why this approach fails and give me some failure test cases
    
  • + 0 comments

    function getMaxLessThanK(n, k) {

    var out_max = 0;
    var out = 0;
    
    for (let i=1; i < n; i++) {
        for (let j=0; j < (n-i); j++) {
            out = i & (i+1)+j; // think of a=i and b=(i+1)+j
            if (out < k && out > out_max) {
                out_max = out;
            }
        }
    }
    return out_max;
    

    }

  • + 0 comments
    function getMaxLessThanK(n, k){
        let maxOfab = 0;
        for (let b = 1; b <= n; b++) {
            for (let a = 1; a < b; a++) {
                maxOfab = (a & b) < k && (a & b) > maxOfab ? a & b : maxOfab
            }
        }
        return maxOfab;
    }
    
  • + 0 comments

    I had difficulty understanding what was to be done (I don't speak English natively).

    So I decided to divide the code like this:

    const getMaxLessThanK = (n,k) => {
        let maxBitwiseValue = 0
        const bitwiseValues = []
    
        for(let a = 1; a < n; a++){
            for(let b = a + 1; b <= n; b++){
                bitwiseValues.push(a & b)
            }
        }
        
        bitwiseValues.forEach(bitwiseValue => {
            if(bitwiseValue < k && bitwiseValue > maxBitwiseValue){
                maxBitwiseValue = bitwiseValue
            }
        })
    
        return maxBitwiseValue   
    }