Count of setbits in bitwise OR of all K length substrings of given Binary String


Set bits are the bits in the binary representation of the number which are '1'. The binary representation of the number contains only two digits '1' and '0', also it may be present in the form of a string. We are given a string, the binary representation of a given number, and an integer k. We have to get all the substrings of the length k from the given string and take the bitwise OR of all of them and at last, we have to return the number of the set bits present in the final string.

Sample Examples

Input

string str = “1100111”
int k = 4

Output

4

Explanation: We have four substrings: 1100, 1001, 0011, and 0111, and the bitwise OR for each of them is 1111 which means we have four set bits present here.

Input

string str = 0110
int k = 4

Output

2

Explanation: We have only one substring which is the given string, so the number of set bits will be 2.

Get All the Substrings

In this approach, we will traverse over the string and get all the substrings by traversing over the string's total length minus the size of the substrings.

We will store the bit present at each index of the final substring by getting the value of a single substring current index that is if it is one then by taking the bitwise OR it will finally be 1.

Example

#include <bits/stdc++.h>
using namespace std;

// creating a function to get the required count 
int getCount(string str, int k){
   int len = str.size(); 
   // defining the vector of size k. it will store the number of set bits at any place. Initially there will be zero at each bit 
   vector<int>bits(k,0); 
   // traversing over the string 
   for(int i=0; i<= len-k; i++){
      // getting each substring
      for(int j =0; j<k;j++){
         if(str[i+j] == '1'){
            // if current substring bit is set bit marking it in the vector
            bits[j] = 1;
         }
      }
   }
   // traversing over the created vector to find the result 
   int ans  = 0;
   for(int i=0; i<k; i++){
      if(bits[i] == 1){
         ans++;
      }
   }
   return ans; // returning the final answer
}
int main(){
   string str = "1100111"; // given string 
   int k = 4; // given number 
   cout<<"The count of setbits in the bitwise OR of the given strings substrings of a given length is: "<<getCount(str,k)<<endl;
}

Output

The count of setbits in the bitwise OR of the given strings substrings of a given length is: 4

Time and Space Complexity

The time complexity of the above code is O(N*N), as we are using nested for loop to get each substring.

The space complexity of the above code is O(N), as we are using an array to store the bits preset at each position.

Using the Difference Array Method

In this approach, we are going to use the difference array method to get the required answer. First, we will create a function to traverse over the string, and in the function, we will create an array to store the difference array results.

We will traverse over the string and for each index we will get the data that which maximum position in the substring and which least position the current index can achieve and will mark that in the difference array.

We will traverse the difference array from the beginning and maintain a count of the number of positives tackles occurred till now. If the current count is greater than zero, then we will count it as the set bit and at last we will return the total number of set bits present in the array.

Example

#include <bits/stdc++.h>
using namespace std;

// creating a function to get the required count 
int getCount(string str, int k){
   int len = str.size(); 
   // defining the vector of size k. it will store the starting and ending of number that will make impact. initially there will be zero at each index 
   vector<int>bits(k+1,0); 
   // traversing over the string 
   for(int i=0; i < len; i++){
      if(str[i] == '0'){
         continue; // this bit will not make any impact 
      }
      // getting starting and ending impact of the current index bit 
      int start; 
      start = max(0,k+i-len);
      int end; 
      end = min(i,k-1);
      // updating value int the difference array 
      bits[start]++; 
      bits[end+1]--;
   }
   // traversing over the created vector to find the result 
   int ans  = 0;
   int tot  = 0;
   for(int i=0; i<k; i++){
      tot += bits[i];
      if(tot > 0){
         ans++;
      }
   }
   return ans; // returning the final answer
}
int main(){
   string str = "0110"; // given string 
   int k = 4; // given number 
   cout<<"The count of set bits in the bitwise OR of the given strings substrings of a given length is: "<<getCount(str,k)<<endl;
}

Output

The count of set bits in the bitwise OR of the given strings substrings of a given length is: 2

Time and Space Complexity

The time complexity of the above code is O(N), as we are traversing over the string just once. Also, we have traversed over the array just once of the same size.

The space complexity of the above code is O(N), as we are using an array to store the difference array results.

Conclusion

In this tutorial, we have implemented a program to find the number of set bits present in the string which is bitwise OR of the substrings or the length k of the given string. We have implemented two programs, first is to find all the substrings and then take the bitwise OR of all of them. The second approach is by using the difference array with the space and time complexity of the O(N).

Updated on: 31-Aug-2023

57 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements