 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Average value of set bit count in given Binary string after performing all possible choices of K operations
In this problem, we need to find the average value of the count of set bits after performing K operations of all choices on the given string.
There can be a brute force approach to solve the problem, but we will use the probability principles to overcome the time complexity of the brute force approach.
Problem statement - We have given an integer N, array arr[] containing K positive integers, and a binary string of length N containing only set bits. We need to find the average value of the count of set bits after performing all possible choices of the K operations. IN ith operation, we can flip any arr[i] a bit of the given string.
Sample examples
Input - N = 2, arr[] = {1, 2}
Output- 1
Explanation- The initial binary string is 11.
- In the first move, we can flip the first character, and the string will be 01. 
- In the second operation, we need to flip any two bits. So the string will be 10. 
- The second choice can start by flipping the second character in the first move, and the string will be 10. 
- In the second move of the current operation, we need to flip any 2 bits, and the string can be 01. 
So, we have 2 choices, and the final strings can be 01 or 10.
Total choices = 2, total set bits in final string = 2, ans = 2/2 = 1.
Input - N = 3, arr[] = {2, 2}
Output- 1.6667
Explanation - We have an initial string is 111.
- In the first operation, we can flip any 2 characters. So, the string can be 001, 100, 010. 
- In the second operation, we can flip the 2 bits in the resultant string we get from the first operation. 
- When we flip any 2 bits of 001, we get 111, 010, and 100. 
- When we flip any 2 bits of 100, we can get 010, 111, and 001. 
- When we flip any 2 bits of 010, we can get 100, 001, and 111. 
So, we got a total 9 different strings in the last operation.
Total set bits in 9 string = 15, total operations = 9, ans = 15/9 = 1.6667
Approach 1
Here we will use the probability principle to solve the problem. Let's assume that the average value of the set bit is p and the off bits is q after performing the i-1 operations. We need to count the average value of set bits and off bits in ith operation.
So, the updated value of p can be p + the average number of new set bits - the average number of new off bits.
Algorithm
- Initialize the P with N as we have N set bits initially and Q with 0 as we have 0 set bits initially. 
- Traverse the array of operations. 
- Initialize the prev_p and prev_q with P and Q values. 
- Update the P value with prev_p - prev_p * arr[i] / N + prev_q * arr[i] / N, which adds average off bits flipped into set bits and removes average set bits flipped into the off bits 
- Update the Q value. 
- Return the P value. 
Example
#include <bits/stdc++.h> using namespace std; double getAverageBits(int len, int K, int array[]) { // to store the average '1's in the binary string double P = len; // to store the average '0's in the binary string double Q = 0; // Traverse the array array[] for (int i = 0; i < K; i++) { // Initialize the prev_p and prev_q with P and Q, which we got from the previous iteration double prev_p = P, prev_q = Q; // Update the average '1's P = prev_p - prev_p * array[i] / len + prev_q * array[i] / len; // Update the average '0's Q = prev_q - prev_q * array[i] / len + prev_p * array[i] / len; } return P; } int main() { int N = 2; int array[] = {1}; int K = sizeof(array) / sizeof(array[0]); cout << "The average number of set bits after performing the operations is " << getAverageBits(N, K, array); return 0; }  Output
The average number of set bits after performing the operations is 1
Time complexity - O(K), where K is the length of the array.
Space complexity - O(1) as we don't use any extra space.
In this tutorial, we learned to find average set bits after performing all possible choices of K operation. In the single choice, we need to perform all operations given in the array.
