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
function averageSetBits ( N , K , arr ) { let p = N ; let q = 0 ; for ( let i = 0 ; i < K ; i ++ ) { let _p = p , _q = q ; p = _p - _p * arr [ i ] / N + _q * arr [ i ] / N ; q = _q - _q * arr [ i ] / N + _p * arr [ i ] / N ; } return p ; } let N = 5 ; let arr = [ 1 , 2 , 3 ] ; let K = arr . length ; document . write ( averageSetBits ( N , K , arr ) . toPrecision ( 3 ) ) ;