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 29: Bitwise AND
Day 29: Bitwise AND
+ 0 comments C# Solution:
public static int bitwiseAnd(int N, int K) { int max = 0; int compare = 0; for(int i = 1; i<N; i++) for(int j = i+1; j<=N; j++) { compare = i&j; max = max < compare && compare < K ? compare : max; } return max; }
+ 1 comment Python
def bitwiseAnd(N, K): max_val = 0 for i in range(K-2,N): for j in range(i+1,N+1): val = i&j if val == K-1: return val if max_val<val<K: max_val = val return max_val
+ 0 comments public static int bitwiseAnd(int N, int K) { int max = Integer.MIN_VALUE; for(int i=1;i<=N;i++){ for(int j=i+1;j<=N;j++){ int result = i&j; if(result < K && result > max) { max = result; } } } return max; }
+ 0 comments JavaScript Solution
function bitwiseAnd(N, K) { let max = 0; for(let i = 1; i <= N; i++) { for(let j = N; j > i; j--) { let bitAndOp = i & j; if(bitAndOp > max && bitAndOp < K ) { max = bitAndOp; } } } return max; }
+ 0 comments Hi, is programmer forced to use the bitwise & operator in the solution code? My solution lacks the concrete use of the operator, but it works based on the features of how it operates on its operands. Shall I use literally the operator in my solution to get passed all test cases?
Load more conversations
Sort 386 Discussions, By:
Please Login in order to post a comment