Skip to content
Related Articles
Check if a pair of integers from two ranges exists such that their Bitwise XOR exceeds both the ranges
• Last Updated : 17 Jun, 2021

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++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to check if there exists``// any pair (P, Q) whose Bitwise XOR``// is greater than the Bitwise XOR``// of X and Y``void` `findWinner(``int` `X, ``int` `Y)``{``    ``// Stores the Bitwise XOR of X & Y``    ``int` `playerA = (X ^ Y);` `    ``bool` `flag = ``false``;` `    ``// Traverse all possible pairs``    ``for` `(``int` `i = 1; i <= X; i++) {` `        ``for` `(``int` `j = 1; j <= Y; j++) {` `            ``int` `val = (i ^ j);` `            ``// If a pair exists``            ``if` `(val > playerA) {``                ``flag = ``true``;``                ``break``;``            ``}``        ``}` `        ``if` `(flag) {``            ``break``;``        ``}``    ``}` `    ``// If a pair is found``    ``if` `(flag) {``        ``cout << ``"Yes"``;``    ``}``    ``else` `{``        ``cout << ``"No"``;``    ``}``}` `// Driver Code``int` `main()``{``    ``int` `A = 2, B = 4;``    ``findWinner(A, B);` `    ``return` `0;``}`

## Python3

 `# Python3 program for the above approach` `# Function to check if there exists``# any pair (P, Q) whose Bitwise XOR``# is greater than the Bitwise XOR``# of X and Y``def` `findWinner(X, Y):``    ` `    ``# Stores the Bitwise XOR of X & Y``    ``playerA ``=` `(X ^ Y)` `    ``flag ``=` `False` `    ``# Traverse all possible pairs``    ``for` `i ``in` `range``(``1``, X ``+` `1``, ``1``):``        ``for` `j ``in` `range``(``1``, Y ``+` `1``, ``1``):``            ``val ``=` `(i ^ j)` `            ``# If a pair exists``            ``if` `(val > playerA):``                ``flag ``=` `True``                ``break` `        ``if` `(flag):``            ``break` `    ``# If a pair is found``    ``if` `(flag):``        ``print``(``"Yes"``)``    ``else``:``        ``print``(``"No"``)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``A ``=` `2``    ``B ``=` `4``    ` `    ``findWinner(A, B)` `# This code is contributed by bgangwar59`
Output:
`No`

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++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to check if there exists``// any pair (P, Q) whose Bitwise XOR``// is greater than the Bitwise XOR``// of X and Y``void` `findWinner(``int` `X, ``int` `Y)``{``    ``int` `first = (X ^ Y);``    ``int` `second = (X + Y);` `    ``// Check for the invalid condition``    ``if` `(first == second) {``        ``cout << ``"No"``;``    ``}` `    ``// Otherwise,``    ``else` `{``        ``cout << ``"Yes"``;``    ``}``}` `// Driver Code``int` `main()``{``    ``int` `A = 2, B = 4;``    ``findWinner(A, B);` `    ``return` `0;``}`

## Python3

 `# Python3 program for the above approach` `# Function to check if there exists``# any pair (P, Q) whose Bitwise XOR``# is greater than the Bitwise XOR``# of X and Y``def` `findWinner(X, Y):``    ` `    ``first ``=` `(X ^ Y)``    ``second ``=` `(X ``+` `Y)` `    ``# Check for the invalid condition``    ``if` `(first ``=``=` `second):``        ``print` `(``"No"``)` `    ``# Otherwise,``    ``else``:``        ``print` `(``"Yes"``)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``A, B ``=` `2``, ``4``    ` `    ``findWinner(A, B)` `# This code is contributed by mohit kumar 29`
Output:
`No`

Time Complexity: O(1)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live

My Personal Notes arrow_drop_up