Average value of set bit count in given Binary string after performing all possible choices of K operations
Given a positive integer N and an array arr[] consisting of K integers and consider a binary string(say S) having N set bits, the task is to find the average value of the count of set bits after performing over all possible choices of K operations on the string S such that in the ith operation, any of the arr[i] bits out of N bits can be flipped.
Example:
Input: N = 3, arr[] = {2, 2}
Output: 1.6667
Explanation: The given binary string having N set bits, let’s say S = ‘111‘. All the possible sequence of moves are as follows:
- In the first move flipping bits S[0] and S[1]. The string will be ‘001‘
- In second move flipping bits S[0] and S[1]. The string will be ‘111’.
- In second move flipping bits S[1] and S[2]. The string will be ‘010‘.
- In second move flipping bits S[0] and S[2]. The string will be ‘100‘.
- In the first move flipping bits S[1] and S[2]. The string will be ‘100‘.
- In second move flipping bits S[0] and S[1]. The string will be ‘010‘.
- In second move flipping bits S[1] and S[2]. The string will be ‘111′.
- In second move flipping bits S[0] and S[2]. The string will be ‘001‘.
- In the first move flipping bits S[0] and S[2]. The string will be ‘010‘.
- In second move flipping bits S[0] and S[1]. The string will be ‘100‘.
- In second move flipping bits S[1] and S[2]. The string will be ‘001‘.
- In second move flipping bits S[0] and S[2]. The string will be ‘111‘.
Therefore, the total number of distinct operations possible are 9 and the count of set bits after the operations in all the cases is 15. So, the average value will be 15/9 = 1.6667.
Input: N = 5, arr[] = {1, 2, 3}
Output: 2.44
Approach: The given problem can be solved using some basic principles of Probability. Suppose, after the (i – 1)th operation, the value of the average number of set bits is p, and the value of the average number of off bits is q. It can be observed that the value of p after the ith operation will become p = p + the average number of off bits flipped into set bits – the average number of set bits flipped into off bits. Since the probability of choosing the bits in the string is equally likely, the value of p can be calculated as pi = pi-1 + q(i – 1)*(arr[i] / N) – p(i – 1)*(arr[i] / N). Follow the steps below to solve the given problem:
- Initialize two variables, say p and q and initially, p = N and q = 0 as all the bits are set bits in the string initially.
- Traverse the given array arr[] using a variable i and update the value of p and q as follows:
- The value of p = p + q*(arr[i] / N) – p*(arr[i] / N).
- Similarly, the value of q = q + p*(arr[i] / N) – q*(arr[i] / N).
- The value stored in p is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
double averageSetBits( int N, int K, int arr[])
{
double p = N;
double q = 0;
for ( int i = 0; i < K; i++) {
double _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;
}
int main()
{
int N = 5;
int arr[] = { 1, 2, 3 };
int K = sizeof (arr) / sizeof (arr[0]);
cout << setprecision(10)
<< averageSetBits(N, K, arr);
return 0;
}
|
Java
import java.io.*;
class GFG {
static double averageSetBits( int N, int K, int arr[])
{
double p = N;
double q = 0 ;
for ( int i = 0 ; i < K; i++) {
double _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;
}
public static void main(String[] args)
{
int N = 5 ;
int arr[] = { 1 , 2 , 3 };
int K = arr.length;
System.out.println(String.format(
"%.10f" , averageSetBits(N, K, arr)));
}
}
|
Python3
def averageSetBits(N, K, arr):
p = N
q = 0
for i in range (K):
_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
if __name__ = = "__main__" :
N = 5
arr = [ 1 , 2 , 3 ]
K = len (arr)
print ( "%.2f" % averageSetBits(N, K, arr))
|
C#
using System;
public class GFG {
static double averageSetBits( int N, int K, int []arr)
{
double p = N;
double q = 0;
for ( int i = 0; i < K; i++) {
double _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;
}
public static void Main(String[] args)
{
int N = 5;
int []arr = { 1, 2, 3 };
int K = arr.Length;
Console.WriteLine(String.Format(
"{0:F10}" , averageSetBits(N, K, arr)));
}
}
|
Javascript
<script>
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));
</script>
|
Time Complexity: O(K), as we are using a loop to traverse N times so it will cost us O(K) time
Auxiliary Space: O(1), as we are not using any extra space.
Last Updated :
04 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...