[Solved] Number of distinct elements in array using bits in int [closed]


The problem is here:

if(0 == maps&(1<<k)){

The equality operator == has higher precedence than the bitwise-AND operator &. So the above evaluates to this:

if ((0 == maps) & (1 << k )) {

You need to add parenthesis to get the desired behavior:

if (0 == (maps & (1 << k))) {

You’ll need to do the same in your cout call:

cout<<" "<<(int)((maps&(1<<k))==0);

1

solved Number of distinct elements in array using bits in int [closed]