Wednesday, 1 Dec 2021
Category: Bitwise-XOR

Given a string S of N characters, the task is to calculate the total number of non-empty substrings such that at most one character occurs an odd number of times.Example: Input: S = “aba”Output: 4Explanation: The valid substrings are “a”, “b”, “a”, and “aba”. Therefore, the total number of required substrings are 4.Input: “aabb”Output: 9Explanation: The […]

Given three numbers N, S and X the task is to find if it is possible to construct a sequence A of length N, where each A[i] >= 0 for 1 X + ( S – X)/2 + (S – X)/2 =  X + (S-X) => X + ( S – X)/2 + (S – […]

Given 3 integers A, B, C, the task is to find the maximum XOR value of three number selected one from each range [0, A], [0, B], [0, C] respectively.Example: Input: A = 1, B = 2, C = 4Output: 7Explanation: Maximum XOR can be calculated by selecting 1 (from [0, 1]), 2 (from [0, 2]) […]

Count of integers K in range [0, N] such that (K XOR K+1) equals (K+2 XOR K+3)Given an integer N, the task is to print the count of all the non-negative integers K less than or equal to N, such that bitwise XOR of K and K+1 equals bitwise XOR of K+2 and K+3.Examples:Input: N […]

#include using namespace std;  string sub(string s){    int n = s.size();    for (int i = n – 1; i >= 0; i–) {                        if (s[i] == ‘0’) {            s[i] = ‘1’;        }        else {                                                s[i] = ‘0’;            break;        }    }        return s;}  string ad(string s){    int n = s.size();      int carry = 0;    for (int i = n – 1; i >= 0; i–) {                                if (s[i] == ‘1’) {            carry = 1;            s[i] […]

Split an array into subarrays with maximum Bitwise XOR of their respective Bitwise OR valuesGiven an array arr[] consisting of N integers, the task is to find the maximum Bitwise XOR of Bitwise OR of every subarray after splitting the array into subarrays(possible zero subarrays).Examples:Input: arr[] = {1, 5, 7}, N = 3Output: 7Explanation:The given […]

Count ways to split array into three non-empty subarrays having equal Bitwise XOR valuesGiven an array arr[] consisting of N non-negative integers, the task is to count the number of ways to split the array into three different non-empty subarrays such that Bitwise XOR of each subarray is equal. Examples:Input: arr[] = {7, 0, 5, 2, […]

#include using namespace std;  int xorSumOfArray(int arr[], int n, int k, int count[]){          int sum = 0;    int p = 1;          for (int i = 0; i < 31; i++) {                          int val = 0;                  if ((k & (1

Find all possible pairs with given Bitwise OR and Bitwise XOR valuesGiven two positive integers A and B representing Bitwise XOR and Bitwise OR of two positive integers, the task is to find all possible pairs (x, y) such that x ^ y is equal to A and x | y is equal to B.Examples:Input: […]

Print all distinct even and odd prefix Bitwise XORs of first N natural numbersGiven a positive integer N, the task is to print all the distinct even and odd values of prefix Bitwise XORs of first N natural numbers.Examples:Input: N = 6Output:Even: 0 4Odd: 1 3 7Explanation:The prefix Bitwise XOR of the first 6 natural […]