Monday, 29 Nov 2021
Category: Bitwise-AND

Given two positive integers X and K, the task is to find the Kth smallest positive integer (Y) such that the sum of X and Y is equal to Bitwise OR of X and Y, i.e. (X + Y = X | Y)Examples:Input: X = 5, K = 1Output: 2Explanation:Consider the smallest number as 2. […]

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;  void findPrefixCount(vector arr,                     int size){        for (int i = 0; i < 32; i++) {                          prefixCount[i]            = ((arr >> 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 = […]

Minimize sum of an array having Bitwise AND of all its pairs present in a given matrixGiven a square symmetric matrix mat[][] of size N, the task is to find the minimum sum possible of an array arr[] of size N, such that for i != j, the value of Bitwise AND of arr[i] and […]

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

Sum of Bitwise AND of the sum of all leaf and non-leaf nodes for each level of a Binary Tree Given a Binary Tree consisting of N nodes, the task is to find the sum of Bitwise AND of the sum of all leaf nodes and the sum of all non-leaf nodes for each level […]