Monday, 29 Nov 2021
Category: Bitwise-AND

Sum of Bitwise AND of all unordered triplets of an arrayGiven an array arr[] consisting of N positive integers, the task is to find the sum of Bitwise AND of all possible triplets (arr[i], arr[j], arr[k]) such that i < j < k.Examples:Input: arr[] = {3, 5, 4, 7}Output: 5Explanation: Sum of Bitwise AND of […]

#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

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 […]

#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 = […]

Check if Bitwise AND of concatenation of diagonals exceeds that of middle row/column elements of a Binary MatrixGiven a binary matrix mat[][] of dimensions N * N, the task is to check if Bitwise AND of the decimal numbers obtained by concatenating primary and secondary diagonals elements is greater than the Bitwise AND of the […]

#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 […]

Count of pairs {X, Y} from an array such that sum of count of set bits in X ⊕ Y and twice the count of set bits in X & Y is M Given an array arr[] consisting of N non-negative integers and an integer M, the task is to find the count of unordered […]