Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Saturday, September 30, 2017

Equal Sum XOR


Here Idea is to count unsetBits as
n+1=n^1 implies that n&i=0
2^unsetbit will be answer


Code:

public int countValues(int n)
{
            int unsetbits=0;
            while(n>0)
            {
                if((n&1)==0)
                ++unsetbits;
                n=n>>1;
            }
            int ans=1;
            ans<<=unsetbits;
            return ans;
}

0 Comments:

Post a Comment

Stats