Count set bits in Bitwise XOR of all adjacent elements upto N | Function to count of set bits in Bitwise XOR of adjacent elements up to N ; Stores count of set bits by Bitwise XOR on adjacent elements of [ 0 , N ] ; Stores all possible values on right most set bit over [ 0 , N ] ; Iterate over the range [ 0 , N ] ; Update N ; Update bit_Position ; Driver Code
def countXORSetBitsAdjElemRange1_N ( N ) : NEW_LINE INDENT total_set_bits = 0 NEW_LINE bit_Position = 1 NEW_LINE while ( N ) : NEW_LINE INDENT total_set_bits += ( ( N + 1 ) // 2 * bit_Position ) NEW_LINE N -= ( N + 1 ) // 2 NEW_LINE bit_Position += 1 NEW_LINE DEDENT return total_set_bits NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE print ( countXORSetBitsAdjElemRange1_N ( N ) ) NEW_LINE DEDENT