Category: setBitCount

Count numbers in the range [L, R] having only three set bits

#include using namespace std;  void precompute(vector& v){        for (long long i = 0; i < 64; i++) {                for (long long j = i + 1; j < 64; j++) {                        for (long long k = j + 1; k < 64; k++) {                                                  long long int x                    = (1LL

Continue Reading

Minimum number of bits of array elements required to be flipped to make all array elements equal

import java.io.*;import java.lang.*;import java.util.*;class GFG{static int makeEqual(int arr[], int n){             int fre0[] = new int[33];        int fre1[] = new int[33];        for(int i = 0; i < n; i++)    {        int x = arr[i];                for(int j = 0; j < 33; j++)        {                        if ((x & 1) != 0)            {                                                 fre1[j] += 1;            }                        else            {                                                 fre0[j] += 1;            }                        x = x >> 1;        }    }        int ans = 0;        for(int i = 0; i […]

Continue Reading

Queries to calculate Bitwise AND of an array with updates

  #include using namespace std;  int prefixCount[32][10000];  void findPrefixCount(vector arr,                     int size){        for (int i = 0; i < 32; i++) {                          prefixCount[i][0]            = ((arr[0] >> i) & 1);                          for (int j = 1; j < size; j++) {                          prefixCount[i][j]                = ((arr[j] >> i) & 1);            prefixCount[i][j]                += prefixCount[i][j – 1];        }    }}  void arrayBitwiseAND(int size){        int result = 0;          for (int i = 0; i < 32; i++) {                          int temp = […]

Continue Reading