Count of pairs having bit size at most X and Bitwise OR equal to X
Last Updated :
16 Sep, 2022
Given a number X, calculate number of possible pairs (a, b) such that bitwise or of a and b is equal to X and number of bits in both a and b is less than equal to number of bits in X.
Examples:
Input: X = 6
Output: 9
Explanation:
The possible pairs of (a, b) are (4, 6), (6, 4), (6, 6), (6, 2), (4, 2), (6, 0), (2, 6), (2, 4), (0, 6).
Input: X = 21
Output: 27
Explanation:
In total there are 27 pairs possible.
Approach: To solve the problem mentioned above follow the steps given below:
- Iterate through every bit of given number X.
- If the bit is 1 then from the truth table of Bitwise OR we know that there are 3 combinations possible for that given bit in number a and b that is (0, 1), (1, 0), (1, 1) that is 3 possible ways.
- If the bit is 0 then from the truth table of Bitwise OR we know that there is only 1 combination possible for that given bit in number a and b that is (0, 0).
- So our answer will be answer will be 3 ^ (number of on bits in X).
Below is the implementation of above approach:
C++
// C++ implementation to Count number of
// possible pairs of (a, b) such that
// their Bitwise OR gives the value X
#include <iostream>
using namespace std;
// Function to count the pairs
int count_pairs(int x)
{
// Initializing answer with 1
int ans = 1;
// Iterating through bits of x
while (x > 0) {
// check if bit is 1
if (x % 2 == 1)
// multiplying ans by 3
// if bit is 1
ans = ans * 3;
x = x / 2;
}
return ans;
}
// Driver code
int main()
{
int X = 6;
cout << count_pairs(X)
<< endl;
return 0;
}
Java
// Java implementation to count number of
// possible pairs of (a, b) such that
// their Bitwise OR gives the value X
class GFG{
// Function to count the pairs
static int count_pairs(int x)
{
// Initializing answer with 1
int ans = 1;
// Iterating through bits of x
while (x > 0)
{
// Check if bit is 1
if (x % 2 == 1)
// Multiplying ans by 3
// if bit is 1
ans = ans * 3;
x = x / 2;
}
return ans;
}
// Driver code
public static void main(String[] args)
{
int X = 6;
System.out.print(count_pairs(X) + "\n");
}
}
// This code is contributed by amal kumar choubey
Python3
# Python3 implementation to count number of
# possible pairs of (a, b) such that
# their Bitwise OR gives the value X
# Function to count the pairs
def count_pairs(x):
# Initializing answer with 1
ans = 1;
# Iterating through bits of x
while (x > 0):
# Check if bit is 1
if (x % 2 == 1):
# Multiplying ans by 3
# if bit is 1
ans = ans * 3;
x = x // 2;
return ans;
# Driver code
if __name__ == '__main__':
X = 6;
print(count_pairs(X));
# This code is contributed by amal kumar choubey
C#
// C# implementation to count number of
// possible pairs of (a, b) such that
// their Bitwise OR gives the value X
using System;
class GFG{
// Function to count the pairs
static int count_pairs(int x)
{
// Initializing answer with 1
int ans = 1;
// Iterating through bits of x
while (x > 0)
{
// Check if bit is 1
if (x % 2 == 1)
// Multiplying ans by 3
// if bit is 1
ans = ans * 3;
x = x / 2;
}
return ans;
}
// Driver code
public static void Main(String[] args)
{
int X = 6;
Console.Write(count_pairs(X) + "\n");
}
}
// This code is contributed by sapnasingh4991
JavaScript
<script>
// javascript implementation to count number of
// possible pairs of (a, b) such that
// their Bitwise OR gives the value X
// Function to count the pairs
function count_pairs(x) {
// Initializing answer with 1
var ans = 1;
// Iterating through bits of x
while (x > 0) {
// Check if bit is 1
if (x % 2 == 1)
// Multiplying ans by 3
// if bit is 1
ans = ans * 3;
x = parseInt(x / 2);
}
return ans;
}
// Driver code
var X = 6;
document.write(count_pairs(X) + "\n");
// This code contributed by Rajput-Ji
</script>
Time complexity: O(log(X))
Auxiliary Space: O(1)
Similar Reads
Count Number of Pairs where Bitwise AND and Bitwise XOR is Equal Given an integer array arr of size N, the task is to count the number of pairs whose BITWISE AND and BITWISE XOR are equal. Example: Input: N = 3, arr[] = {0,0,1}Output: 1Explanation: There is only one pair arr[0] and arr[1] as 0&0=0 and 0^0=0 Input: N = 4, arr[] = {1, 2, 4, 8}Output: 0Explanati
4 min read
Count pairs with equal Bitwise AND and Bitwise OR value Given an array, arr[] of size N, the task is to count the number of unordered pairs such that Bitwise AND and Bitwise OR of each pair is equal. Examples: Input: arr[] = {1, 2, 1} Output: 1 Explanation: Bitwise AND value and Bitwise OR value all possible pairs are: Bitwise AND of the pair(arr[0], arr
6 min read
Non-negative pairs with sum of Bitwise OR and Bitwise AND equal to N Given an integer N, the task is to find all non-negative pairs (A, B) such that the sum of Bitwise OR and Bitwise AND of A, B is equal to N, i.e., (A | B) + (A & B) = N. Examples: Input: N = 5Output: (0, 5), (1, 4), (2, 3), (3, 2), (4, 1), (5, 0)Explanation: All possible pairs satisfying the nec
5 min read
Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively Given two integers X and Y, the task is to find the total number of ways to generate a pair of integers A and B such that Bitwise XOR and Bitwise AND between A and B is X and Y respectively Examples: Input: X = 2, Y = 5Output: 2Explanation:The two possible pairs are (5, 7) and (7, 5).Pair 1: (5, 7)B
7 min read
Count values whose Bitwise OR with A is equal to B Given two integers A and B, the task is to count possible values of X that satisfies the condition A | X = B. Note: | represents Bitwise OR operation. Examples: Input: A = 2, B = 3Output: 2Explanation: Since, 2 | 1 = 3 and 2 | 3 = 3. Therefore, the possible values of x are 1 and 3. Input: A = 5, B =
6 min read
Count of pairs from Array with sum equal to twice their bitwise AND Given an array arr[], the task is to count the pairs in the array with sum equal to twice their bitwise AND, i.e., A + B = 2 * (A \& B) Examples: Input: arr[] = {1, 1, 3, 4, 4, 5, 7, 8} Output: 2 Explanation: Pairs with sum equal to twice their bitwise AND: {(1, 1), (4, 4)}Input: arr[] = {1, 3,
7 min read