Given 3 integers a, b and c, Find the minimum value of x such that the expression ((a|x) & (b|x)) = c holds, else, return -1.
Note: Here '|' means bitwise OR and '&' means bitwise AND operation.
Examples:
Input: a = 8, b = 11, c = 9
Output: 1
Explanation: a|x = 8|1 = 9 and b|x = 11|1 = 11 , ((a|x) & (b|x)) = 11&9 = 9, which is equal to c. Thus, x=1. It can be proved that no value of x is possible which is less than 1, and it follows the equality.
Input: a = 3, b = 2, c = 1
Output: -1
Explanation: No such x exists which follows the mentioned equality.
[Naive Approach] Brute Force - O(231) Time and O(1) Space
The naive method works by checking every possible value of x starting from zero. For each value, it evaluates the expression (a | x) & (b | x) and compares the result with c. If the expression becomes equal to c, that x is returned immediately. This guarantees correctness, but the process is extremely slow. Since x may go up to billions, this method is not practical for large values.
C++
#include <iostream>
using namespace std;
int evalExpression(int a, int b, int c) {
// try all x values from 0 to 2^31
for (long long x = 0; x < (1LL << 31); x++) {
// check expression
if (((a | x) & (b | x)) == c)
return (int)x;
}
return -1;
}
int main() {
int a = 8, b = 11, c = 9;
cout << evalExpression(a, b, c);
return 0;
}
Java
class GFG {
static int evalExpression(int a, int b, int c) {
// try all x values from 0 to 2^31
for (long x = 0; x < (1L << 31); x++) {
// check expression
if (((a | x) & (b | x)) == c)
return (int)x;
}
return -1;
}
public static void main(String[] args) {
int a = 8, b = 11, c = 9;
System.out.println(evalExpression(a, b, c));
}
}
Python
def evalExpression(a, b, c):
# try all x values from 0 to 2^31
for x in range(1 << 31):
# check expression
if ((a | x) & (b | x)) == c:
return x
return -1
if __name__ == "__main__":
a, b, c = 8, 11, 9
print(evalExpression(a, b, c))
C#
using System;
class GFG
{
static int evalExpression(int a, int b, int c)
{
// try all x values from 0 to 2^31 - 1
for (long x = 0; x < (1L << 31); x++)
{
// check expression
if (((a | (int)x) & (b | (int)x)) == c)
return (int)x;
}
return -1;
}
static void Main()
{
int a = 8, b = 11, c = 9;
Console.WriteLine(evalExpression(a, b, c));
}
}
JavaScript
function evalExpression(a, b, c) {
// try all x values from 0 to 2^31
for (let x = 0n; x < (1n << 31n); x++) {
// check expression
if (((BigInt(a) | x) & (BigInt(b) | x)) === BigInt(c))
return Number(x);
}
return -1;
}
// Driver Code
let a = 8, b = 11, c = 9;
console.log(evalExpression(a, b, c));
[Expected Approach] Bitwise Logic - O(32) Time and O(1) Space
The optimal method is based on simplifying the original expression using bitwise logic. The expression (a | x) & (b | x) can be reduced to the form (a & b) | x, which makes the condition easier to analyze. If any bit of (a & b) is 1 while the same bit in c is 0, then no value of x can satisfy the equation. Otherwise, the bits of x must match the bits of c wherever (a & b) has a zero. This creates the smallest valid x and allows the solution to be computed efficiently in constant time per bit.
C++
#include <iostream>
using namespace std;
int evalExpression(int a, int b, int c) {
// precompute a & b
int d = a & b;
long long x = 0;
for (int i = 0; i < 31; i++) {
// extract bits
int D = (d >> i) & 1;
int C = (c >> i) & 1;
// impossible when D=1 and C=0
if (D == 1 && C == 0)
return -1;
// when D=0, x must copy c
if (D == 0 && C == 1)
x |= (1LL << i);
}
return (int)x;
}
int main() {
int a = 8, b = 11, c = 9;
cout << evalExpression(a, b, c);
return 0;
}
Java
class GFG {
static int evalExpression(int a, int b, int c) {
// precompute a & b
int d = a & b;
long x = 0;
for (int i = 0; i < 31; i++) {
// extract bits
int D = (d >> i) & 1;
int C = (c >> i) & 1;
// impossible when D=1 and C=0
if (D == 1 && C == 0)
return -1;
// when D=0, x must copy c
if (D == 0 && C == 1)
x |= (1L << i);
}
return (int)x;
}
public static void main(String[] args) {
int a = 8, b = 11, c = 9;
System.out.println(evalExpression(a, b, c));
}
}
Python
def evalExpression(a, b, c):
# precompute a & b
d = a & b
x = 0
for i in range(31):
# extract bits
D = (d >> i) & 1
C = (c >> i) & 1
# impossible when D=1 and C=0
if D == 1 and C == 0:
return -1
# when D=0, x must copy c
if D == 0 and C == 1:
x |= (1 << i)
return x
if __name__ == "__main__":
a, b, c = 8, 11, 9
print(evalExpression(a, b, c))
C#
using System;
class GFG
{
static int evalExpression(int a, int b, int c)
{
// precompute a & b
int d = a & b;
long x = 0;
for (int i = 0; i < 31; i++)
{
// extract bits
int D = (d >> i) & 1;
int C = (c >> i) & 1;
// impossible when D=1 and C=0
if (D == 1 && C == 0)
return -1;
// when D=0, x must copy c
if (D == 0 && C == 1)
x |= (1L << i);
}
return (int)x;
}
static void Main()
{
int a = 8, b = 11, c = 9;
Console.WriteLine(evalExpression(a, b, c));
}
}
JavaScript
function evalExpression(a, b, c) {
// precompute a & b
let d = (a & b);
let x = 0n;
for (let i = 0n; i < 31n; i++) {
// extract bits
let D = (BigInt(d) >> i) & 1n;
let C = (BigInt(c) >> i) & 1n;
// impossible when D=1 and C=0
if (D === 1n && C === 0n)
return -1;
// when D=0, x must copy c
if (D === 0n && C === 1n)
x |= (1n << i);
}
return Number(x);
}
// Driver Code
let a = 8, b = 11, c = 9;
console.log(evalExpression(a, b, c));
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem