- Bit Magic, Bitwise-OR, Bitwise-XOR, Mathematical, Searching

Find all possible pairs with given Bitwise OR and Bitwise XOR values

Find all possible pairs with given Bitwise OR and Bitwise XOR valuesGiven two positive integers A and B representing Bitwise XOR and Bitwise OR of two positive integers, the task is to find all possible pairs (x, y) such that x ^ y is equal to A and x | y is equal to B.Examples:Input: A = 5, B = 7Output:2 73 66 37 2Explanation:7( XOR )2 = 5 and 7( OR )2 = 73( XOR )6 = 5 and 3( OR )6 = 7Input: A = 8, B = 10Output:2 1010 2Naive Approach: The simplest approach to solve the problem is to generate all possible pairs and for each pair, check if their Bitwise XOR and Bitwise OR are equal to A and B respectively.Time Complexity: O(B2)Auxiliary Space: O(1)Efficient Approach: The idea is to traverse through all possible values of x and use the property of XOR that if x ^ y = A, then x ^ A = y to find all possible values of y. Follow the steps below to solve the problem:Iterate from 1 to B using a variable, say i, and perform the following operations:Initialize a variable y as i ^ A.Check if the value of y is greater than 0 and (i | y) is equal to B or not.If found to be true, then print the values of i and y.Below is the implementation of the above approach:C++#include using namespace std;  void findPairs(int A, int B){        for (int i = 1; i 0 and (i | y) == B) {            cout