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.
- Prepare
- Algorithms
- Bit Manipulation
- AND Product
- Discussions
AND Product
AND Product
+ 0 comments Java 8
public static long andProduct(long a, long b) { long sum = a & (a+1); for (long i = a+2; i <= b-1; i++) { sum = sum & i ; } return sum; }
+ 1 comment Python 3:
def andProduct(a, b): if a == 0: return 0 if a == b: return a out = None if a % 2 == 0 else a if out is not None: a+=1 temp = a def check_less_significant_bits(num): shift = 0 zero=True cum_sum=0 n_sum=0 while zero: zero = (num & ( 1 << shift )) == 0 if zero: cum_sum += 2**shift shift+=1 return cum_sum + 1 a += check_less_significant_bits(a) while a <= b: temp &= a if temp == 0: return 0 inc = check_less_significant_bits(a) a+=inc if out is not None: return temp & out return temp
+ 1 comment def andProduct(a, b): n=0 while a != b: a>>=1 b>>=1 n+=1 return a<<n
+ 0 comments long andProduct(long a, long b) { if ((b-a) >> 1) return a & (a+1); return a; }
Can someone tell me why am I failing the hidden testcases?
+ 1 comment long andProduct(long a, long b) { long n=a; long i=0; while (a
return n; }
Load more conversations
Sort 194 Discussions, By:
Please Login in order to post a comment