New to manipulation

bits
manipulation

#1

hello everyone i am fairly new to bits.I was trying to solve 2 questions…1st one and 2nd one i was able to fetch the 1st one with 52 points but i have no idea to get 100pts and i am facing difficulty with its editorial and not even getting the second question.can anybody tell me how to get comfortable with these type of questions as i have seen many many times in the contests.Is this called bits masking??If yes can anybody help me to understand these concepts… :slight_smile: :slight_smile:


#2

I think you should know about Binary numbers and conversions of binary to decimal…Google it if you donot know…
I can help you with the first question it does not uses bitmasking… AND has a property that if both the bits are 1 then only it will return…
Clearly N is so high it is impossible to pass all cases in O(N^2)
What we do in that question is breakdown each number into bits in so called bit array and we keep on summing it … I mean take A* change it into binary representation and add 1s to the corresponding places in binary notation… Now We can compute it in O(32*N) assuming int to be 32 bits … Now the grreat thing is we will get number of bits that were both true in that position when we Choose 2 from any bit place…Hence the number of pairs which will result in an 1 in binary notation is simply = Number of true bits in that position Choose 2… Now we have teh pairs we can multiply it to 2^the bit place (Changing Binary number to decimal)…