We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Day 6: Bitwise Operators
Day 6: Bitwise Operators
Sort by
recency
|
227 Discussions
|
Please Login in order to post a comment
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) { 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; }
This was my answer
function getMaxLessThanK(n,k){ let maxValue = 0; for(let i = 1; i
}