Category: Bitwise-AND

Rearrange an array to maximize sum of Bitwise AND of same-indexed elements with another array

#include using namespace std;int maximizeAnd(int i, int mask,                int* A, int* B, int N,                vector& dp){        if (i == N)        return 0;            if (dp[i][mask] != -1)        return dp[i][mask];        for (int j = 0; j < N; ++j) {                        if (!(mask & (1

Continue Reading

Bitwise OR of Bitwise AND of all subarrays of an array

Bitwise OR of Bitwise AND of all subarrays of an arrayGiven an array arr[] consisting of N positive integers, the task is to find the Bitwise OR of Bitwise AND of all subarrays of the given array.Examples:Input: arr[] = {1, 2, 3}Output: 3Explanation:Following are the Bitwise AND of all possible subarrays are:{1}, Bitwise AND is […]

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

Connect a graph by M edges such that the graph does not contain any cycle and Bitwise AND of connected vertices is maximum

#include using namespace std;    int maximumAND(int arr[], int n, int m) {               int tot = 1 > i) & 1) {                                  if (count == 0) {                                          andans = arr[i];                 }                 else {                                                               andans = andans & arr[i];                 }                                                      count++;             }         }                              if […]

Continue Reading