Check if any Array pair has bitwise XOR greater than bitwise AND
Last Updated :
12 Apr, 2023
Given an array arr[] of size N, the task is to find if there exists a pair in the array, such that their bitwise XOR is greater than their bitwise AND i.e. arr[i] ⊕ arr[j] > arr[i] & arr[j], (0 ≤ i < j ≤ N-1) where ⊕ represents the Bitwise XOR operator and & represents bitwise AND operator.
Examples:
Input: arr[] = {4, 5, 8, 6}
Output: Yes
Explanation: Bitwise XOR of 4 and 8 is 12 and bitwise AND = 0.
Input: arr[] = {5, 4, 7, 6}
Output: No
Naive Approach: The approach to this problem is to find all the possible pairs and check if any of the pairs has bitwise XOR greater than bitwise AND.
C++
#include <bits/stdc++.h>
using namespace std;
bool checkPair( int * arr, int N)
{
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N; j++) {
int xorr = arr[i] ^ arr[j];
int and = arr[i] & arr[j];
if (xorr > and)
return true ;
}
}
return false ;
}
int main()
{
int arr[] = { 4, 5, 8, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
if (checkPair(arr, N))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class Gfg {
static boolean checkPair( int [] arr, int N)
{
for ( int i = 0 ; i < N; i++) {
for ( int j = i + 1 ; j < N; j++) {
int xorr = arr[i] ^ arr[j];
int and = arr[i] & arr[j];
if (xorr > and)
return true ;
}
}
return false ;
}
public static void main(String[] args)
{
int [] arr = { 4 , 5 , 8 , 6 };
int N = arr.length;
if (checkPair(arr, N))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def checkPair( arr, N):
for i in range ( 0 ,N):
for j in range (i + 1 ,N):
xorr = arr[i] ^ arr[j];
and = arr[i] & arr[j];
if (xorr > and ):
return True ;
return False ;
arr = [ 4 , 5 , 8 , 6 ];
N = len (arr);
if (checkPair(arr, N)):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class Gfg {
static bool checkPair( int [] arr, int N)
{
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N; j++) {
int xorr = arr[i] ^ arr[j];
int and = arr[i] & arr[j];
if (xorr > and)
return true ;
}
}
return false ;
}
public static void Main()
{
int [] arr = { 4, 5, 8, 6 };
int N = arr.Length;
if (checkPair(arr, N))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
function checkPair( arr, N)
{
for (let i = 0; i < N; i++) {
for (let j = i + 1; j < N; j++) {
let xorr = arr[i] ^ arr[j];
let and = arr[i] & arr[j];
if (xorr > and)
return true ;
}
}
return false ;
}
let arr = [ 4, 5, 8, 6 ];
let N = arr.length;
if (checkPair(arr, N))
console.log( "Yes" );
else
console.log( "No" );
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The problem can be solved efficiently based on the following idea:
The idea is built on the observation that:
- Bitwise XOR of two same bits is always 0, i.e. 1⊕1 = 0⊕0 = 0 and XOR of two different bit is 1.
- Bitwise AND of two same bits are same as that bit i.e. 1⊕1 = 1 and 0⊕0 = 0 and AND of different bits is always 0.
Now from the above observation, it can be said that if the MSB (Most significant bit) of two numbers are at different positions then their bitwise XOR will be greater than bitwise AND because.
- The MSB will be the XOR of two different bits which will result in a set bit and bitwise AND of two different bits will be a 0.
- The MSB of bitwise XOR will be a greater power than the MSB of bitwise AND.
So to find if such a pair is possible check the conditions only for the minimum and the maximum of the array because they are the extreme values of the array. If they have the MSB in the same positions then all other between them will have the MSB in that position and bitwise XOR will never be greater than the bitwise AND for any pair. In all other cases such a pair is possible.
Follow the steps mentioned below to implement the approach:
- Initialize Min and Max to store the maximum and minimum elements of the array.
- Traverse the array arr[] from i = 0 to N-1:
- Store the maximum element in Max and the minimum element in Min.
- Now check the number of count bits of Min and Max.
- If count of Bit of Max ≠count of Bit of Min, then print YES (because then MSB of Max will be in a different position than MSB of Min).
- Else print NO.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countBit( int n)
{
int Count = 0;
while (n) {
n /= 2;
Count++;
}
return Count;
}
bool checkPair( int * arr, int N)
{
int Min = INT_MAX;
int Max = INT_MIN;
for ( int i = 0; i < N; i++) {
Min = min(Min, arr[i]);
Max = max(Max, arr[i]);
}
if (countBit(Min) != countBit(Max))
return true ;
else
return false ;
}
int main()
{
int arr[] = { 4, 5, 8, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
if (checkPair(arr, N))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static int countBit( int n)
{
int Count = 0 ;
while (n != 0 ) {
n /= 2 ;
Count++;
}
return Count;
}
public static boolean checkPair( int arr[], int N)
{
int Min = Integer.MAX_VALUE;
int Max = Integer.MIN_VALUE;
for ( int i = 0 ; i < N; i++) {
Min = Math.min(Min, arr[i]);
Max = Math.max(Max, arr[i]);
}
if (countBit(Min) != countBit(Max))
return true ;
else
return false ;
}
public static void main(String[] args)
{
int arr[] = { 4 , 5 , 8 , 6 };
int N = arr.length;
if (checkPair(arr, N) == true )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
import sys
def countBit(n):
Count = 0
while (n):
n = n / / 2
Count + = 1
return Count
def checkPair(arr,N):
Min = sys.maxsize
Max = - sys.maxsize - 1
for i in range (N):
Min = min ( Min , arr[i])
Max = max ( Max , arr[i])
if (countBit( Min ) ! = countBit( Max )):
return True
else :
return False
arr = [ 4 , 5 , 8 , 6 ]
N = len (arr)
if (checkPair(arr, N)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
public static int countBit( int n)
{
int Count = 0;
while (n != 0) {
n /= 2;
Count++;
}
return Count;
}
public static bool checkPair( int [] arr, int N)
{
int Minn = Int32.MaxValue;
int Maxx = Int32.MinValue;
for ( int i = 0; i < N; i++) {
Minn = Math.Min(Minn, arr[i]);
Maxx = Math.Max(Maxx, arr[i]);
}
if (countBit(Minn) != countBit(Maxx))
return true ;
else
return false ;
}
public static void Main()
{
int [] arr = { 4, 5, 8, 6 };
int N = arr.Length;
if (checkPair(arr, N) == true )
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function countBit(n)
{
let Count = 0;
while (n != 0) {
n /= 2;
Count++;
}
return Count;
}
function checkPair(arr, N)
{
let Min = Number.MAX_VALUE;
let Max = Number.MIN_VALUE;
for (let i = 0; i < N; i++) {
Min = Math.min(Min, arr[i]);
Max = Math.max(Max, arr[i]);
}
if (countBit(Min) != countBit(Max))
return true ;
else
return false ;
}
let arr = [ 4, 5, 8, 6 ];
let N = arr.length;
if (checkPair(arr, N) == true )
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...