Average value of set bit count in given Binary string after performing all possible choices of K operations | Function to calculate the average number of Set bits after after given operations ; Stores the average number of set bits after current operation ; Stores the average number of set bits after current operation ; Iterate through the array arr [ ] and update the values of p and q ; Store the value of p and q of the previous state before their updation ; Update average number of set bits after performing the ith operation ; Update average number of off bits after performing the ith operation ; Return Answer ; Driver Code
def averageSetBits ( N , K , arr ) : NEW_LINE INDENT p = N NEW_LINE q = 0 NEW_LINE for i in range ( K ) : NEW_LINE INDENT _p = p NEW_LINE _q = q NEW_LINE p = _p - _p * arr [ i ] / N + _q * arr [ i ] / N NEW_LINE q = _q - _q * arr [ i ] / N + _p * arr [ i ] / N NEW_LINE DEDENT return p NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE arr = [ 1 , 2 , 3 ] NEW_LINE K = len ( arr ) NEW_LINE print ( " % .2f " % averageSetBits ( N , K , arr ) ) NEW_LINE DEDENT