Count of possible pairs whose sum and bitwise XOR is given
Last Updated :
28 Jun, 2022
Given two integers S and X representing the sum and bitwise XOR respectively of two integers, the task is to find the count of all such possible pairs such that their sum is equal to S and bitwise XOR is equal to X.
Examples:
Input: S = 9, X = 5
Output: 4
Explanation: (2, 7), (3, 6), (6, 3), (7, 2) completely satisfies the given conditions.
It can also be seen that no other pair satisfy the given condition.
So the total possibility is 4.
Input: S = 3, X = 3
Output: 4
Explanation: Only (1, 2), (2, 1), (3, 0) and (0, 3) satisfy the given condition.
Approach: The solution to the problem is based on the following observation:
Consider two numbers a and b. Their sum can be written as
a+b = (a XOR b) + (a AND b)*2
The above can be proved as follows:
If two bits x and y are added, the carry (say c) will be (x AND y) and it is to one position left of the single bit. So, c = 2 * (x AND y)
The value of the rightmost bit (i.e., at the same position as of the single bits) will be (x XOR y)
Therefore x + y = (x XOR y) + 2 * (x AND y)
So when adding a and b, this procedure repeats for each bit. Therefore it can be said that the sum will be
a+b = (a XOR b) + (a AND b)*2
S = X + 2*A where A is the bitwise AND of the two numbers.
A = (S - X)/2
Based on the above observation the count can be obtained from the bit values of A and X as per the following case
- If the ith bit of X and A both are 0, then there is only one possible pair for that bit position.
- If the ith bit of X is 1 and of A is 0, then there are two possible pairs for that bit position.
- If the ith bit of X is 0 and of A is 1, then there is only one possible pair for that bit position.
- There cannot be a situation where ith bit at X is 1 and ith bit of A is also 1.
According to the multiplication principle of permutation, to get the number of possible pairs satisfying the given condition just multiply all possibilities. Follow the steps mentioned below to implement the idea:
- Find bitwise AND of two numbers by using the above formula.
- If it is not an integer then no solution exist
- Otherwise for given value of the XOR (X) and AND (say A) check for each bit
- Find the number of possibilities for that bit using the above cases.
- Multiply these possibilities with the final answer.
- At the end the total number possibilities calculated as per the above conditions is the required answer.
Below is the implementation of the above approach.
C++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to calculate
// total number of possible pairs
int count_all_possible_pair(int sum_value,
int xor_value)
{
double and_value
= ((sum_value - xor_value) / 2.0);
// If and value is not an integer
// then no pair is possible
int check = and_value;
if (check != and_value)
return 0;
int count = 1;
// Traversing the bits of the sum_value
// from MSB position
for (int i = 1; i <= 32; i++) {
int and_bit = (check >> i) & 1;
int xor_bit = (xor_value >> i) & 1;
// If both the bit is 0, only 1 possibility
if (and_bit == 0 && xor_bit == 0)
count = count * 1;
// If Xor bit is 0 and And bit 1,
// then only 1 possibility
else if (xor_bit == 0 && and_bit == 1)
count = count * 1;
// If Xor bit is 1, And bit is 0,
// then there are 2 possibilities
else if (xor_bit == 1 && and_bit == 0)
count = count * 2;
// If Xor bit and And bit both 1,
// no such case is possible
else
return 0;
}
// Return the count of possible pairs
return count;
}
// Driver code
int main()
{
int S = 9, X = 5;
// Function call
cout << count_all_possible_pair(S, X);
return 0;
}
Java
// Java code to implement the approach
public class GFG {
// Function to calculate
// total number of possible pairs
static int count_all_possible_pair(int sum_value,
int xor_value)
{
double and_value
= ((sum_value - xor_value) / 2.0);
// If and value is not an integer
// then no pair is possible
int check = (int)and_value;
if (check != and_value)
return 0;
int count = 1;
// Traversing the bits of the sum_value
// from MSB position
for (int i = 1; i <= 32; i++) {
int and_bit = (check >> i) & 1;
int xor_bit = (xor_value >> i) & 1;
// If both the bit is 0, only 1 possibility
if (and_bit == 0 && xor_bit == 0)
count = count * 1;
// If Xor bit is 0 and And bit 1,
// then only 1 possibility
else if (xor_bit == 0 && and_bit == 1)
count = count * 1;
// If Xor bit is 1, And bit is 0,
// then there are 2 possibilities
else if (xor_bit == 1 && and_bit == 0)
count = count * 2;
// If Xor bit and And bit both 1,
// no such case is possible
else
return 0;
}
// Return the count of possible pairs
return count;
}
// Driver code
public static void main (String[] args)
{
int S = 9, X = 5;
// Function call
System.out.println(count_all_possible_pair(S, X));
}
}
// This code is contributed by AnkThon
Python3
# Python3 code to implement the approach
# Function to calculate
# total number of possible pairs
def count_all_possible_pair(sum_value, xor_value) :
and_value = ((sum_value - xor_value) / 2.0);
# If and value is not an integer
# then no pair is possible
check = int(and_value);
if (check != and_value) :
return 0;
count = 1;
# Traversing the bits of the sum_value
# from MSB position
for i in range(1 , 33) :
and_bit = ((check >> i) & 1);
xor_bit = ((xor_value >> i) & 1);
# If both the bit is 0, only 1 possibility
if (and_bit == 0 and xor_bit == 0) :
count = count * 1;
# If Xor bit is 0 and And bit 1,
# then only 1 possibility
elif (xor_bit == 0 and and_bit == 1) :
count = count * 2;
# If Xor bit is 1, And bit is 0,
# then there are 2 possibilities
elif (xor_bit == 1 and and_bit == 0) :
count = count * 2;
# If Xor bit and And bit both 1,
# no such case is possible
else :
return 0;
# Return the count of possible pairs
return count;
# Driver code
if __name__ == "__main__" :
S = 9; X = 5;
# Function call
print(count_all_possible_pair(S, X));
# This code is contributed by AnkThon
C#
// C# code to implement the approach
using System;
public class GFG {
// Function to calculate
// total number of possible pairs
static int count_all_possible_pair(int sum_value,
int xor_value)
{
double and_value
= ((sum_value - xor_value) / 2.0);
// If and value is not an integer
// then no pair is possible
int check = (int)and_value;
if (check != and_value)
return 0;
int count = 1;
// Traversing the bits of the sum_value
// from MSB position
for (int i = 1; i <= 32; i++) {
int and_bit = (check >> i) & 1;
int xor_bit = (xor_value >> i) & 1;
Console.WriteLine("value:" + i);
Console.WriteLine(and_bit+"test"+xor_bit);
// If both the bit is 0, only 1 possibility
if (and_bit == 0 && xor_bit == 0)
count = count * 1;
// If Xor bit is 0 and And bit 1,
// then only 1 possibility
else if (xor_bit == 0 && and_bit == 1)
count = count * 1;
// If Xor bit is 1, And bit is 0,
// then there are 2 possibilities
else if (xor_bit == 1 && and_bit == 0)
count = count * 2;
// If Xor bit and And bit both 1,
// no such case is possible
else
return 0;
}
// Return the count of possible pairs
return count;
}
// Driver code
public static void Main ()
{
int S = 9, X = 5;
// Function call
Console.Write(count_all_possible_pair(S, X));
}
}
// This code is contributed by gfgking
JavaScript
// JavaScript code to implement the approach
// Function to calculate
// total number of possible pairs
function count_all_possible_pair(sum_value, xor_value)
{
var and_value = Math.floor((sum_value - xor_value) / 2.0);
// If and value is not an integer
// then no pair is possible
var check = and_value;
if (check != and_value)
return 0;
var count = 1;
// Traversing the bits of the sum_value
// from MSB position
for (var i = 1; i <= 32; i++) {
var and_bit = (check >> i) & 1;
var xor_bit = (xor_value >> i) & 1;
// If both the bit is 0, only 1 possibility
if (and_bit == 0 && xor_bit == 0)
count = count * 1;
// If Xor bit is 0 and And bit 1,
// then only 1 possibility
else if (xor_bit == 0 && and_bit == 1)
count = count * 1;
// If Xor bit is 1, And bit is 0,
// then there are 2 possibilities
else if (xor_bit == 1 && and_bit == 0)
count = count * 2;
// If Xor bit and And bit both 1,
// no such case is possible
else
return 0;
}
// Return the count of possible pairs
return count;
}
// Driver code
var S = 9;
var X = 5;
// Function call
document.write(count_all_possible_pair(S, X));
// This code is contributed by phasing17
Time Complexity: O(N) where N is the number of bits in the given sum S
Auxiliary Space: O(1)
Similar Reads
Count of pairs whose bitwise AND is a power of 2
Given an array arr[] of N positive integers. The task is to find the number of pairs whose Bitwise AND value is a power of 2.Examples: Input: arr[] = {2, 1, 3, 4} Output: 2 Explanation: There are 2 pairs (2, 3) and (1, 3) in this array whose Bitwise AND values are: 1. (2 & 3) = 1 = (20) 2. (1
13 min read
XOR of all possible pairwise sum from two given Arrays
Given two arrays A[] and B[] of equal length, the task is to find the Bitwise XOR of the pairwise sum of the given two arrays. Examples: Input: A[] = {1, 2}, B[] = {3, 4} Output: 2 Explanation: Sum of all possible pairs are {4(1 + 3), 5(1 + 4), 5(2 + 3), 6(2 + 4)} XOR of all the pair sums = 4 ^ 5 ^
15+ min read
Find all possible pairs with given Bitwise OR and Bitwise XOR values
Given 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: A = 5, B = 7Output:2 73 66 37 2Explanation:7( XOR )2 = 5 and 7( OR )2 = 73( XOR
8 min read
Count of all possible values of X whose Bitwise XOR with N is greater than N
Given an integer N count the number of values of X such that XâN > N, where â denotes bitwise XOR operation Examples: Input: N = 10Output: 5 Explanation: The five possible value satisfying the above condition are:1â10 = 11, 4â10 = 14, 5â10 = 15, 6â10 = 12, 7â10 = 13 Input: N = 8Output: 7Explanati
6 min read
Count of pairs with bitwise XOR value greater than its bitwise AND value
Given an array arr that contains N positive Integers. Find the count of all possible pairs whose bitwise XOR value is greater than bitwise AND value Examples: Input : arr[]={ 12, 4, 15}Output: 2Explanation: 12 ^ 4 = 8, 12 & 4 = 4. so 12 ^ 4 > 12 & 4 4 ^ 15 = 11, 4 & 15 = 4. so 4 ^ 15
4 min read
Sum of Bitwise AND of all pairs possible from two arrays
Given two arrays A[] and B[] of size N and M respectively, the task is to find the sum of Bitwise AND of all possible unordered pairs (A[i], B[j]) from the two arrays. Examples: Input: A[] = {1, 2} , B[] = {3, 4} Output: 3 Explanation: Bitwise AND of all possible pairs are 1 & 3 = 1 1 & 4 =
5 min read
Count of Possible paths of given Matrix having Bitwise XOR equal to K
Given an N*M (N + M ⤠40)matrix mat[][], each cell has some value ranging from 0 to 1018 and an integer K. Find the count of all possible paths such that bitwise XOR of elements in a path is equal to K. Movement is allowed only in the right and downward direction. Examples: Input: N = 3, M = 4, K= 2
15+ min read
Number of unique pairs whose bitwise XOR and OR are same
Given an integer N, the task is to find the number of pairs (say {a, b})from 1 to N (both inclusive) that satisfies the condition: a and b are distinct elements. The values of a | b and a ^ b are equal and a( a | b ) = b( a ^ b ) - ( a | b ) also holds true. Examples: Input: N=5Output:1Explanation:
9 min read
Count of pairs in Array such that bitwise AND of XOR of pair and X is 0
Given an array arr[] consisting of N positive integers and a positive integer X, the task is to find the number of pairs (i, j) such that i < j and (arr[i]^arr[j] )&X is 0. Examples: Input: arr[] = {1, 3, 4, 2}, X = 2 Output: 2Explanation:Following are the possible pairs from the given array:
11 min read
Count distinct possible Bitwise XOR values of subsets of an array
Given an array arr[] consisting of N integers, the task is to find the size of the set S such that Bitwise XOR of any subset of the array arr[] exists in the set S. Examples: Input: arr[] = {1, 2, 3, 4, 5}Output: 8Explanation:All possible Bitwise XOR values of subsets of the array arr[] are {0, 1, 2
14 min read