Here is the link to my code :
Please help me solve this code
I am not sure about your idea, but ans basically is just number of set bits.