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.
def bitwiseAnd(N, K):
max_and = 0
for b in range(2, N + 1):
for a in range(1, b):
current_and = a & b
if max_and < current_and < K:
max_and = current_and
# Optimization: if we hit K-1, we found the highest possible value
if max_and == K - 1:
return max_and
return max_and
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 29: Bitwise AND
You are viewing a single comment's thread. Return to all comments →
def bitwiseAnd(N, K): max_and = 0 for b in range(2, N + 1): for a in range(1, b): current_and = a & b if max_and < current_and < K: max_and = current_and # Optimization: if we hit K-1, we found the highest possible value if max_and == K - 1: return max_and return max_and