given an array of non-negative integers, count the number of unordered pairs of array elements such that their bitwise AND(&) is a power of 2.
for ex: for array [10,7,2,8,3]; there are 6 unordered pairs;
explaination:
10&7=2
10&2=2;
10&8=2;
10&3=2;
7&2=2;
2&3=2;
Constraints:
1<=n<=2*10^5
0<=ar[i]<=2^12
This was asked in Hackerank intermediate certification Test.I failed to come up with linear time solution.
Maybe we should use something like sum over subsets dp. Not sure though.
Since the numbers are small (<=4096), you can simply try every pair of possible numbers and check if they are a valid pair. If they are just add to the answer how many times you have this pair in your array (if the numbers are $$$A$$$ and $$$B$$$, you need to add count($$$A$$$)*count($$$B$$$),attention if $$$A=B$$$). Complexity $$$O(Amax*Amax)$$$
During the contest i did'nt observed that constraint carefully, that's the mistake i made. Well thanks for Helping BTW. Solution
I think your code is doing wrong calculations.
If
i==j
,ans
should increment bymp[i]*(mp[i]-1)/2
which is the number of combinations when picking 2 out of mp[i] objects.Take example of
{1,1,1,1}
. The answer should be 6(=4*3/2) and not 4.If
i!=j
,ans
should increment bymp[i]*mp[j]
.Here's my code, which passed all test cases.