[Hacker Rank] AND Product

AND Product

  • Time : O(log(min(a,b)))
  • Space : O(1)
1
2
3
4
5
6
long andProduct(long a, long b) {
if(!a or !b) return 0;
long lga = log2(a), lgb = log2(b);
if(lga == lgb) return (1ll<<lga) + andProduct(a ^ (1ll<<lga), b ^ (1ll<<lga));
return 0;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/14/PS/HackerRank/and-product/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.