Count number of set bits in a range using bitset
Given a large binary number.The task is to count the number of 1’s in a given range from L to R (1 based indexing).
Examples:
Input : s = “101101011010100000111”, L = 6, R = 15
Output : 5
s [L : R] = “1011010100”
There is only 5 set bits.
Input : s = “10110”, L = 2, R = 5
Output : 2
Approach:
- Convert the string of size len to the bitset of size N.
- There is no need of (N – len) + (L – 1) bits in the left side and (N – R) bits in the right side of the bitset .
- Remove those bits efficiently using left and right shift bitwise operation.
- Now there are all zeroes in the left side of L and right side of R, so just use count() function to get the count of 1’s in the bitset as all positions except [L, R] are ‘0’.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 32
int GetOne(string s, int L, int R)
{
int len = s.length();
bitset<N> bit(s);
bit <<= (N - len + L - 1);
bit >>= (N - len + L - 1);
bit >>= (len - R);
return bit.count();
}
int main()
{
string s = "01010001011" ;
int L = 2, R = 4;
cout << GetOne(s, L, R);
return 0;
}
|
Java
public class Main {
static final int N = 32 ;
static int getOne(String s, int L, int R) {
int len = s.length();
s = "0" .repeat(Math.max( 0 , N - len)) + s;
String bit = s.substring(N - R, N - L + 1 );
return ( int ) bit.chars().filter(c -> c == '1' ).count();
}
public static void main(String[] args) {
String s = "01010001011" ;
int L = 2 , R = 4 ;
System.out.println(getOne(s, L, R));
}
}
|
Python3
N = 32
def binStrToInt(binary_str):
length = len (binary_str)
num = 0
for i in range (length):
num = num + int (binary_str[i])
num = num * 2
return num / 2
def GetOne(s, L, R) :
length = len (s);
bit = s.zfill( 32 - len (s));
bit = int (binStrToInt(bit))
bit << = (N - length + L - 1 );
bit >> = (N - length + L - 1 );
bit >> = (length - R);
return bin (bit).count( '1' );
if __name__ = = "__main__" :
s = "01010001011" ;
L = 2 ; R = 4 ;
print (GetOne(s, L, R));
|
C#
using System;
using System.Linq;
class GFG {
static int N = 32;
static int GetOne( string s, int L, int R)
{
int len = s.Length;
s = s.PadLeft(N, '0' );
string bit = s.Substring(N - R, R - L + 1);
return bit.Count(f => (f == '1' ));
}
public static void Main( string [] args)
{
string s = "01010001011" ;
int L = 2, R = 4;
Console.WriteLine(GetOne(s, L, R));
}
}
|
Javascript
var N = 32;
function binStrToInt(binary_str)
{
var length = binary_str.length;
var num = 0;
for ( var i = 0; i < length; i++) {
num = num + parseInt(binary_str[i]);
num = num * 2;
}
return num / 2;
}
function GetOne(s, L, R)
{
var length = s.length;
var bit = "0" * (32 - length) + s;
bit = parseInt(binStrToInt(bit));
bit <<= (N - length + L - 1);
bit >>= (N - length + L - 1);
bit >>= (length - R);
return bit.toString(2).split( "1" ).length - 1;
}
var s = "01010001011" ;
var L = 2;
var R = 4;
console.log(GetOne(s, L, R));
|
Time Complexity: O(len)
Auxiliary Space: O(len)
Last Updated :
20 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...