Given an array of positive integer and q query which contains two integers, L & R. Task is to find the number of set bits for a given range.
Prerequisite : Bitwise Hacks
Examples :
Input : Arr[] = { 1, 5, 6, 10, 9, 4 }
Query : 2
L & R
1 5
2 4
Output : 9
6
Input : Arr[] = { 1, 10, 5, 2, 8, 11, 15 }
Query : 2
L & R
2 4
1 5
Output : 4
9
Simple solution to this problem is to run a loop from L to R and count number of set bits in a Range. This solution take O(nlog(s)) ( where s is bits size ) for each query.
Efficient solution is based on the fact that if we store count of all set bits of numbers in an array “BitCounts”, then we answer each query in O(1) time. So, start traversing the elements of array and count set bits for each element and store in array. Now, find cumulative sum of this array. This array will help in answering queries.
BitCount[] that will store the count of set bits
in a number.
Run a Loop from 0 to 31 "for 32 bits size integer "
-> mark elements with i'th bit set
Run an inner Loop from 0 to size of Array "Arr"
-> Check whether the current bit is set or not
-> if it's set then mark it.
long temp = arr[j] >> i;
if (temp %2 != 0)
BitCount[j] += 1
Below is implementation of above idea.
C++
#include <bits/stdc++.h>
using namespace std;
int BitCount[10000] = { 0 };
void fillSetBitsMatrix( int arr[], int n)
{
for ( int i = 0; i < 32; i++) {
for ( int j = 0; j < n; j++) {
long temp = arr[j] >> i;
if (temp % 2 != 0)
BitCount[j] += 1;
}
}
for ( int i = 1; i < n; i++)
BitCount[i] += BitCount[i - 1];
}
void Query( int Q[][2], int q)
{
for ( int i = 0; i < q; i++)
cout << (BitCount[Q[i][1]] -
BitCount[Q[i][0] - 1]) << endl;
}
int main()
{
int Arr[] = { 1, 5, 6, 10, 9, 4, 67 };
int n = sizeof (Arr) / sizeof (Arr[0]);
fillSetBitsMatrix(Arr, n);
int q = 2;
int Q[2][2] = { { 1, 5 }, { 2, 6 } };
Query(Q, q);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int BitCount[] = new int [ 10000 ];
static void fillSetBitsMatrix( int arr[], int n)
{
for ( int i = 0 ; i < 32 ; i++) {
for ( int j = 0 ; j < n; j++) {
long temp = arr[j] >> i;
if (temp % 2 != 0 )
BitCount[j] += 1 ;
}
}
for ( int i = 1 ; i < n; i++)
BitCount[i] += BitCount[i - 1 ];
}
static void Query( int Q[][], int q)
{
for ( int i = 0 ; i < q; i++)
System.out.println( (BitCount[Q[i][ 1 ]]
- BitCount[Q[i][ 0 ] - 1 ]));
}
public static void main (String[] args)
{
int Arr[] = { 1 , 5 , 6 , 10 , 9 , 4 , 67 };
int n = Arr.length;
fillSetBitsMatrix(Arr, n);
int q = 2 ;
int Q[][] = { { 1 , 5 }, { 2 , 6 } };
Query(Q, q);
}
}
|
Python3
BitCount = [ 0 ] * 10000
def fillSetBitsmatrix(arr: list , n: int ):
global BitCount
for i in range ( 32 ):
for j in range (n):
temp = arr[j] >> i
if temp % 2 ! = 0 :
BitCount[j] + = 1
for i in range ( 1 , n):
BitCount[i] + = BitCount[i - 1 ]
def Query(Q: list , q: int ):
for i in range (q):
print (BitCount[Q[i][ 1 ]] - BitCount[Q[i][ 0 ] - 1 ])
if __name__ = = "__main__" :
Arr = [ 1 , 5 , 6 , 10 , 9 , 4 , 67 ]
n = len (Arr)
fillSetBitsmatrix(Arr, n)
q = 2
Q = [( 1 , 5 ), ( 2 , 6 )]
Query(Q, q)
|
C#
using System;
class GFG {
static int []BitCount = new int [10000];
static void fillSetBitsMatrix( int []arr, int n)
{
for ( int i = 0; i < 32; i++) {
for ( int j = 0; j < n; j++) {
long temp = arr[j] >> i;
if (temp % 2 != 0)
BitCount[j] += 1;
}
}
for ( int i = 1; i < n; i++)
BitCount[i] += BitCount[i - 1];
}
static void Query( int [,]Q, int q)
{
for ( int i = 0; i < q; i++)
Console.WriteLine( (BitCount[Q[i,1]]
- BitCount[Q[i,0] - 1]));
}
public static void Main ()
{
int []Arr = { 1, 5, 6, 10, 9, 4, 67 };
int n = Arr.Length;
fillSetBitsMatrix(Arr, n);
int q = 2;
int [,]Q = { { 1, 5 }, { 2, 6 } };
Query(Q, q);
}
}
|
Javascript
<script>
var BitCount = Array.from({length: 10000}, (_, i) => 0);
function fillSetBitsMatrix(arr, n)
{
for (i = 0; i < 32; i++)
{
for (j = 0; j < n; j++)
{
var temp = arr[j] >> i;
if (temp % 2 != 0)
BitCount[j] += 1;
}
}
for (i = 1; i < n; i++)
BitCount[i] += BitCount[i - 1];
}
function Query(Q, q)
{
for (i = 0; i < q; i++)
document.write((BitCount[Q[i][1]] -
BitCount[Q[i][0] - 1]) + "<br>" );
}
var Arr = [ 1, 5, 6, 10, 9, 4, 67 ];
var n = Arr.length;
fillSetBitsMatrix(Arr, n);
var q = 2;
var Q = [ [ 1, 5 ], [ 2, 6 ] ];
Query(Q, q);
</script>
|
Time Complexity : O(1) for each query.
Auxiliary Space: O(k) where k=10000