Check if a pair of integers from two ranges exists such that their Bitwise XOR exceeds both the ranges
Last Updated :
28 Jul, 2022
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 <bits/stdc++.h>
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;
}
C
// C program for the above approach
#include <stdio.h>
#include <stdbool.h>
// 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)
printf("Yes");
else
printf("No");
}
// Driver Code
int main()
{
int A = 2, B = 4;
findWinner(A, B);
return 0;
}
// This code is contributed by Sania Kumari Gupta
Java
// Java program for the above approach
import java.lang.*;
import java.util.*;
class GFG{
// Function to check if there exists
// any pair (P, Q) whose Bitwise XOR
// is greater than the Bitwise XOR
// of X and Y
static void findWinner(int X, int Y)
{
// Stores the Bitwise XOR of X & Y
int playerA = (X ^ Y);
boolean 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)
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
}
// Driver code
public static void main(String[] args)
{
int A = 2, B = 4;
findWinner(A, B);
}
}
// This code is contributed by offbeat
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
C#
// C# program for the above approach
using System.Collections.Generic;
using System;
class GFG{
// Function to check if there exists
// any pair (P, Q) whose Bitwise XOR
// is greater than the Bitwise XOR
// of X and Y
static 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)
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("No");
}
}
// Driver code
public static void Main(String[] args)
{
int A = 2, B = 4;
findWinner(A, B);
}
}
// This code is contributed by amreshkumar3
JavaScript
<script>
// JavaScript 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
function findWinner(X,Y)
{
// Stores the Bitwise XOR of X & Y
let playerA = (X ^ Y);
let flag = false;
// Traverse all possible pairs
for(let i = 1; i <= X; i++)
{
for(let j = 1; j <= Y; j++)
{
let val = (i ^ j);
// If a pair exists
if (val > playerA)
{
flag = true;
break;
}
}
if (flag)
{
break;
}
}
// If a pair is found
if (flag)
{
document.write("Yes<br>");
}
else
{
document.write("No<br>");
}
}
// Driver code
let A = 2, B = 4;
findWinner(A, B);
// This code is contributed by unknown2108
</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++
// C++ program for the above approach
#include <bits/stdc++.h>
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;
}
Java
// Java program for the above approach
import java.lang.*;
import java.util.*;
class GFG{
// Function to check if there exists
// any pair (P, Q) whose Bitwise XOR
// is greater than the Bitwise XOR
// of X and Y
static void findWinner(int X, int Y)
{
int first = (X ^ Y);
int second = (X + Y);
// Check for the invalid condition
if (first == second)
{
System.out.println("No");
}
// Otherwise,
else
{
System.out.println("Yes");
}
}
// Driver code
public static void main(String[] args)
{
int A = 2, B = 4;
findWinner(A, B);
}
}
// This code is contributed by offbeat
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
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if there exists
// any pair (P, Q) whose Bitwise XOR
// is greater than the Bitwise XOR
// of X and Y
static void findWinner(int X, int Y)
{
int first = (X ^ Y);
int second = (X + Y);
// Check for the invalid condition
if (first == second)
{
Console.Write("No");
}
// Otherwise,
else
{
Console.Write("Yes");
}
}
// Driver code
public static void Main(String[] args)
{
int A = 2, B = 4;
findWinner(A, B);
}
}
// This code is contributed by shivanisinghss2110
JavaScript
<script>
// JavaScript 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
function findWinner(X,Y)
{
let first = (X ^ Y);
let second = (X + Y);
// Check for the invalid condition
if (first == second) {
document.write("No");
}
// Otherwise,
else {
document.write("Yes");
}
}
// Driver Code
let A = 2, B = 4;
findWinner(A, B);
// This code is contributed by patel2127
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Find pair of integers such that their sum is twice their Bitwise XOR Given a positive integer N, the task is to find all pairs of integers (i, j) from the range [1, N] in increasing order of i such that: 1 ? i, j ? Ni + j = Ni + j = 2*(i ^ j)If there are no such pairs, return a pair {-1, -1}. Note: Here '^' denotes the bitwise XOR operation. Examples: Input: N = 4Out
6 min read
Count pairs with Bitwise XOR greater than both the elements of the pair Given an array arr[] of size N, the task is to count the number of pairs whose Bitwise XOR is greater than both the elements in the pair. Examples: Input: arr[] = {2, 4, 3}Output: 2Explanation: There are only 2 pairs whose Bitwise XOR is greater than both the elements in the pair:1) (2, 4): Bitwise
10 min read
Find K numbers in a given range [L, R] such that their bitwise XOR is X Given four numbers L, R, K, and X, the task is to find K distinct decimal numbers in the range [L, R] such that their bitwise XOR is X. Note: If there are more than one possibilities, print any one of them. Examples: Input: L = 1 , R = 13, K = 5, X = 11Output: 2 3 8 9 11Explanation: 2 â 3 â 8 â 9 â
7 min read
Maximize count of pairs whose Bitwise AND exceeds Bitwise XOR by replacing such pairs with their Bitwise AND Given an array arr[] consisting of N positive integers, replace pairs of array elements whose Bitwise AND exceeds Bitwise XOR values by their Bitwise AND value. Finally, count the maximum number of such pairs that can be generated from the array. Examples: Input: arr[] = {12, 9, 15, 7}Output: 2Expla
9 min read
Check if there exists some positive integers X and Y such that P^X is equal to Q^Y Given two integers P and Q, the task is to check whether a pair (P, Q) is equal or not, and a pair is said to be equal if there exist some positive integers X and Y such that PX = QY. Examples: Input: P = 16 , Q = 4Output: Yes?Explanation: Let X = 2 and Y = 4. Thus, PX = 162 = 256 and QY = 44 = 256
4 min read