Count pairs from a given range having equal Bitwise OR and XOR values
Last Updated :
27 Oct, 2021
Given an integer N, the task is to find the count total number of pairs (P, Q) from the range 0 ≤ P, Q < 2N, such that P OR Q = P XOR Q. Since the count can be very large, print it to modulo 109 + 7.
Examples:
Input: N = 1
Output: 3
Explanation: The pairs (P, Q) satisfying P OR Q = P XOR Q are (0, 0), (0, 1) and (1, 0). Hence output 3.
Input: N = 3
Output: 27
Naive Approach: The simplest approach is to generate all possible pairs (P, Q) and count the pairs satisfying P OR Q = P XOR Q.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
long long int power( int x, int y)
{
long long int res = 1;
x = x % MOD;
while (y > 0) {
if (y & 1)
res = (res * x) % MOD;
y = y >> 1;
x = (x * x) % MOD;
}
return res;
}
void countPairs( int N)
{
long long int high = power(2, N);
int count = 0;
for ( int i = 0; i < high; i++) {
for ( int j = 0; j < high; j++) {
int X = (i ^ j);
int Y = (i | j);
if (X == Y) {
count++;
}
}
}
cout << count % MOD << endl;
}
int main()
{
int N = 10;
countPairs(N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int MOD = 1000000007 ;
static int power( int x, int y)
{
int res = 1 ;
x = x % MOD;
while (y > 0 )
{
if ((y & 1 ) != 0 )
res = (res * x) % MOD;
y = y >> 1 ;
x = (x * x) % MOD;
}
return res;
}
static void countPairs( int N)
{
int high = power( 2 , N);
int count = 0 ;
for ( int i = 0 ; i < high; i++)
{
for ( int j = 0 ; j < high; j++)
{
int X = (i ^ j);
int Y = (i | j);
if (X == Y)
{
count++;
}
}
}
System.out.println(count % MOD);
}
public static void main(String[] args)
{
int N = 10 ;
countPairs(N);
}
}
|
Python3
def power(x, y):
MOD = 1000000007
res = 1
x = x % MOD
while (y > 0 ):
if (y & 1 ):
res = (res * x) % MOD
y = y >> 1
x = (x * x) % MOD
return res
def countPairs( N):
MOD = 1000000007
high = power( 2 , N)
count = 0
for i in range (high):
for j in range (high):
X = (i ^ j)
Y = (i | j)
if (X = = Y):
count + = 1
print (count % MOD)
N = 10
countPairs(N)
|
C#
using System;
class GFG{
static int MOD = 1000000007;
static int power( int x, int y)
{
int res = 1;
x = x % MOD;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % MOD;
y = y >> 1;
x = (x * x) % MOD;
}
return res;
}
static void countPairs( int N)
{
int high = power(2, N);
int count = 0;
for ( int i = 0; i < high; i++)
{
for ( int j = 0; j < high; j++)
{
int X = (i ^ j);
int Y = (i | j);
if (X == Y)
{
count++;
}
}
}
Console.WriteLine(count % MOD);
}
static public void Main()
{
int N = 10;
countPairs(N);
}
}
|
Javascript
<script>
MOD = 1000000007;
function power(x , y) {
var res = 1;
x = x % MOD;
while (y > 0) {
if ((y & 1) != 0)
res = (res * x) % MOD;
y = y >> 1;
x = (x * x) % MOD;
}
return res;
}
function countPairs(N) {
var high = power(2, N);
var count = 0;
for (i = 0; i < high; i++) {
for (j = 0; j < high; j++) {
var X = (i ^ j);
var Y = (i | j);
if (X == Y) {
count++;
}
}
}
document.write(count % MOD);
}
var N = 10;
countPairs(N);
</script>
|
Time Complexity: O(22*N)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is based on the following observations:
- Consider the pair as (P, Q). Fix P and then find all the Qs which satisfy this equation. So, all the bits which are set in P will be unset in Q.
- For each unset bit in P, there are two options i.e., the corresponding bits in Q can be 0 or 1.
- So for any P, if the number of unset bits in P is u(P) then the number of Q’s will be ans1 = 2^u(P).
- Iterate over the range [0, N] using the variable i, then for each 2i will have a contribution of NCi. Let this count be ans2.
- So the count of pairs is given by ∑(ans1*ans2) = (1 + 2)N = 3N.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
long long int power( int x, int y)
{
long long int res = 1;
x = x % MOD;
while (y > 0) {
if (y & 1)
res = (res * x) % MOD;
y = y >> 1;
x = (x * x) % MOD;
}
return res;
}
void countPairs( int N)
{
cout << power(3, N);
}
int main()
{
int N = 10;
countPairs(N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static final int MOD = 1000000007 ;
static int power( int x, int y)
{
int res = 1 ;
x = x % MOD;
while (y > 0 )
{
if (y % 2 == 1 )
res = (res * x) % MOD;
y = y >> 1 ;
x = (x * x) % MOD;
}
return res;
}
static void countPairs( int N)
{
System.out.print(power( 3 , N));
}
public static void main(String[] args)
{
int N = 10 ;
countPairs(N);
}
}
|
Python3
MOD = 1000000007 ;
def power(x, y):
res = 1 ;
x = x % MOD;
while (y > 0 ):
if (y % 2 = = 1 ):
res = (res * x) % MOD;
y = y >> 1 ;
x = (x * x) % MOD;
return res;
def countPairs(N):
print (power( 3 , N));
if __name__ = = '__main__' :
N = 10 ;
countPairs(N);
|
C#
using System;
public class GFG
{
static readonly int MOD = 1000000007;
static int power( int x, int y)
{
int res = 1;
x = x % MOD;
while (y > 0)
{
if (y % 2== 1)
res = (res * x) % MOD;
y = y >> 1;
x = (x * x) % MOD;
}
return res;
}
static void countPairs( int N)
{
Console.Write(power(3, N));
}
public static void Main(String[] args)
{
int N = 10;
countPairs(N);
}
}
|
Javascript
<script>
var MOD = 1000000007;
function power(x , y) {
var res = 1;
x = x % MOD;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % MOD;
y = y >> 1;
x = (x * x) % MOD;
}
return res;
}
function countPairs(N) {
document.write(power(3, N));
}
var N = 10;
countPairs(N);
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...