Check if a pair of integers from two ranges exists such that their Bitwise XOR exceeds both the ranges
Given two integers A and B, the task is to check if there exists two integers P and Q over the range [1, A] and [1, B] respectively such that Bitwise XOR of P and Q is greater than A and B. If found to be true, then print “Yes”. Otherwise, print “No”.
Examples:
Input: X = 2, Y = 2
Output: Yes
Explanation:
By choosing the value of P and Q as 1 and 2 respectively, gives the Bitwise XOR of P and Q as 1^2 = 3 which is greater than Bitwise XOR of A and B A ^ B = 0.
Therefore, print Yes.
Input: X = 2, Y = 4
Output: No
Naive Approach: The simplest approach to solve the given problem is to generate all possible pairs of (P, Q) by traversing all integers from 1 to X and 1 to Y and check if there exists a pair such that their Bitwise XOR is greater than Bitwise XOR of X and Y, then print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findWinner( int X, int Y)
{
int playerA = (X ^ Y);
bool flag = false ;
for ( int i = 1; i <= X; i++) {
for ( int j = 1; j <= Y; j++) {
int val = (i ^ j);
if (val > playerA) {
flag = true ;
break ;
}
}
if (flag)
break ;
}
if (flag)
cout << "Yes" ;
else
cout << "No" ;
}
int main()
{
int A = 2, B = 4;
findWinner(A, B);
return 0;
}
|
C
#include <stdio.h>
#include <stdbool.h>
void findWinner( int X, int Y)
{
int playerA = (X ^ Y);
bool flag = false ;
for ( int i = 1; i <= X; i++) {
for ( int j = 1; j <= Y; j++) {
int val = (i ^ j);
if (val > playerA) {
flag = true ;
break ;
}
}
if (flag)
break ;
}
if (flag)
printf ( "Yes" );
else
printf ( "No" );
}
int main()
{
int A = 2, B = 4;
findWinner(A, B);
return 0;
}
|
Java
import java.lang.*;
import java.util.*;
class GFG{
static void findWinner( int X, int Y)
{
int playerA = (X ^ Y);
boolean flag = false ;
for ( int i = 1 ; i <= X; i++)
{
for ( int j = 1 ; j <= Y; j++)
{
int val = (i ^ j);
if (val > playerA)
{
flag = true ;
break ;
}
}
if (flag)
{
break ;
}
}
if (flag)
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
public static void main(String[] args)
{
int A = 2 , B = 4 ;
findWinner(A, B);
}
}
|
Python3
def findWinner(X, Y):
playerA = (X ^ Y)
flag = False
for i in range ( 1 , X + 1 , 1 ):
for j in range ( 1 , Y + 1 , 1 ):
val = (i ^ j)
if (val > playerA):
flag = True
break
if (flag):
break
if (flag):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
A = 2
B = 4
findWinner(A, B)
|
C#
using System.Collections.Generic;
using System;
class GFG{
static void findWinner( int X, int Y)
{
int playerA = (X ^ Y);
bool flag = false ;
for ( int i = 1; i <= X; i++)
{
for ( int j = 1; j <= Y; j++)
{
int val = (i ^ j);
if (val > playerA)
{
flag = true ;
break ;
}
}
if (flag)
{
break ;
}
}
if (flag)
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
public static void Main(String[] args)
{
int A = 2, B = 4;
findWinner(A, B);
}
}
|
Javascript
<script>
function findWinner(X,Y)
{
let playerA = (X ^ Y);
let flag = false ;
for (let i = 1; i <= X; i++)
{
for (let j = 1; j <= Y; j++)
{
let val = (i ^ j);
if (val > playerA)
{
flag = true ;
break ;
}
}
if (flag)
{
break ;
}
}
if (flag)
{
document.write( "Yes<br>" );
}
else
{
document.write( "No<br>" );
}
}
let A = 2, B = 4;
findWinner(A, B);
</script>
|
Time Complexity: O(X * Y)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized based on the following observations:
- For any two integers P and Q, the maximum Bitwise XOR value is (P + Q) which can only be found when there are no common bits between P and Q in their binary representation.
- There are two cases:
- Case 1: If player A has two integers that produce the maximum Bitwise XOR value, then print “No”.
- Case 2: In this case, there must have some common bit between A and B such that there always exist two integers P and Q whose Bitwise XOR is always greater than the Bitwise XOR of A and B, where (P ^ Q) = (X | Y).
Therefore, from the above observations, the idea is to check if the value of given A^B is equal to A + B or not. If found to be true, then print “No”. Otherwise, print “Yes”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findWinner( int X, int Y)
{
int first = (X ^ Y);
int second = (X + Y);
if (first == second) {
cout << "No" ;
}
else {
cout << "Yes" ;
}
}
int main()
{
int A = 2, B = 4;
findWinner(A, B);
return 0;
}
|
Java
import java.lang.*;
import java.util.*;
class GFG{
static void findWinner( int X, int Y)
{
int first = (X ^ Y);
int second = (X + Y);
if (first == second)
{
System.out.println( "No" );
}
else
{
System.out.println( "Yes" );
}
}
public static void main(String[] args)
{
int A = 2 , B = 4 ;
findWinner(A, B);
}
}
|
Python3
def findWinner(X, Y):
first = (X ^ Y)
second = (X + Y)
if (first = = second):
print ( "No" )
else :
print ( "Yes" )
if __name__ = = '__main__' :
A, B = 2 , 4
findWinner(A, B)
|
C#
using System;
class GFG{
static void findWinner( int X, int Y)
{
int first = (X ^ Y);
int second = (X + Y);
if (first == second)
{
Console.Write( "No" );
}
else
{
Console.Write( "Yes" );
}
}
public static void Main(String[] args)
{
int A = 2, B = 4;
findWinner(A, B);
}
}
|
Javascript
<script>
function findWinner(X,Y)
{
let first = (X ^ Y);
let second = (X + Y);
if (first == second) {
document.write( "No" );
}
else {
document.write( "Yes" );
}
}
let A = 2, B = 4;
findWinner(A, B);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
28 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...