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=5
Output:1
Explanation: If we consider a=3 and b=4, then a | b = 7 and a ^ b=7. Thus it forms a valid pair
Input: N=8
Output: 3
The possible pairs are {1, 2}, {3, 4} and {7, 8}
Intuition:
Let us consider two numbers a and b. As we know, the sum of these two values can be written as:
- a + b = ( a | b ) + ( a & b )
- a + b = ( a ^ b ) + 2*( a & b)
So equating 1 and 2 we get:
a | b + (a & b) = a^b + 2 * (a & b) or
a | b = a ^ b + (a & b) (equation 3)
Since it is said that (a ^ b) = (a | b), from third condition, we can derive:
a * ( a | b ) = b * ( a ^ b ) – ( a | b ) or
a * (a | b) = b * (a | b) – (a | b) or
a = b – 1 i.e. b – a = 1
Since (a ^ b) and (a | b) are same, so from the 3rd equation we can derive that (a & b) = 0.
So the question finally boils down to figure out the adjacent pair of elements whose bitwise AND is 0.
Naive Approach using Linear Iteration:
The basic idea to solve this problem is to store count of unique adjacent pairs by iterating over the array using a loop from 1 to N.
- Initialize a counter variable (say count) to store the count of unique pairs.
- Start traversing using from i = 1 to N-1:
- Find the bitwise AND of i and i+1 and if it is 0 increment the count.
- Return count as the required answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int countPairs( int n)
{
int count = 0;
for ( int i = 1; i < n; i++) {
int ans = (i & (i + 1));
if (ans == 0)
count++;
}
return count;
}
int main()
{
int N = 8;
cout << "For N = 8: " << countPairs(N) << "\n" ;
N = 1;
cout << "For N = 1: " << countPairs(N) << "\n" ;
N = 2;
cout << "For N = 2: " << countPairs(N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countPairs( int n)
{
int count = 0 ;
for ( int i = 1 ; i < n; i++) {
int ans = (i & (i + 1 ));
if (ans == 0 ) {
count++;
}
}
return count;
}
public static void main(String[] args)
{
int N = 8 ;
System.out.println( "For N = 8: " + countPairs(N));
N = 1 ;
System.out.println( "For N = 1: " + countPairs(N));
N = 2 ;
System.out.println( "For N = 2: " + countPairs(N));
}
}
|
Python3
def countPairs(n):
count = 0
for i in range ( 1 , n):
ans = (i & (i + 1 ))
if ans = = 0 :
count = count + 1
return count
if __name__ = = "__main__" :
N = 8
print ( "For N = 8: " , countPairs(N))
N = 1
print ( "For N = 1: " , countPairs(N))
N = 2
print ( "For N = 2: " , countPairs(N))
|
C#
using System;
public class GFG {
static int countPairs( int n)
{
int count = 0;
for ( int i = 1; i < n; i++) {
int ans = (i & (i + 1));
if (ans == 0) {
count++;
}
}
return count;
}
static public void Main()
{
int N = 8;
Console.WriteLine( "For N = 8: " + countPairs(N));
N = 1;
Console.WriteLine( "For N = 1: " + countPairs(N));
N = 2;
Console.WriteLine( "For N = 2: " + countPairs(N));
}
}
|
Javascript
const countPairs = (n) => {
let count = 0;
for (let i = 1; i < n; i++) {
let ans = (i & (i + 1));
if (ans == 0)
count++;
}
return count;
}
let N = 8;
console.log(`For N = 8: ${countPairs(N)}<br/>`);
N = 1;
console.log(`For N = 1: ${countPairs(N)}<br/>`);
N = 2;
console.log(`For N = 2: ${countPairs(N)}`);
|
Output
For N = 8: 3
For N = 1: 0
For N = 2: 1
Time Complexity: O(N)
Auxiliary Space: O(N)
Efficient Approach:
Since we know that both the numbers differ by 1 hence by the property of the AND operation of any power of 2 with a number that is one less than that gives 0. Hence (2n) & (2n-1)==0, So we can directly count the number of pairs of form 2n and 2n-1.
Below is the implementation of the above idea.
C++
#include <bits/stdc++.h>
using namespace std;
int countPairs( int n)
{
int max_power_of_2 = log2(n);
return max_power_of_2;
}
int main()
{
int N = 8;
cout << "For N = 8: " << countPairs(N) << "\n" ;
N = 1;
cout << "For N = 1: " << countPairs(N) << "\n" ;
N = 2;
cout << "For N = 2: " << countPairs(N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int countPairs( int n)
{
int max_power_of_2 = ( int )(Math.log(n) / Math.log( 2 ));
return max_power_of_2;
}
public static void main (String[] args) {
int N = 8 ;
System.out.println( "For N = 8: " + countPairs(N));
N = 1 ;
System.out.println( "For N = 1: " + countPairs(N));
N = 2 ;
System.out.println( "For N = 2: " + countPairs(N));
}
}
|
Python3
import math
def countPairs(n):
max_power_of_2 = int (math.log2(n))
return max_power_of_2
N = 8
print ( "For N = 8: " ,countPairs(N))
N = 1
print ( "For N = 1: " ,countPairs(N))
N = 2
print ( "For N = 2: " ,countPairs(N))
|
C#
using System;
class GFG {
static int countPairs( int n)
{
int max_power_of_2
= ( int )(Math.Log(n) / Math.Log(2));
return max_power_of_2;
}
public static void Main( string [] args)
{
int N = 8;
Console.WriteLine( "For N = 8: " + countPairs(N));
N = 1;
Console.WriteLine( "For N = 1: " + countPairs(N));
N = 2;
Console.WriteLine( "For N = 2: " + countPairs(N));
}
}
|
Javascript
function countPairs(n)
{
let max_power_of_2 = Math.round(Math.log2(n));
return max_power_of_2;
}
let N = 8;
console.log( "For N = 8: " +countPairs(N));
N = 1;
console.log( "For N = 1: " +countPairs(N));
N = 2;
console.log( "For N = 2: " +countPairs(N));
|
Output
For N = 8: 3
For N = 1: 0
For N = 2: 1
Time Complexity: O(log N) since it calculates in log (N) time.
Space Complexity: O(1)
Related Articles:
Last Updated :
15 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...