Given two positive integers **A** and **B** such that **A != B**, the task is to find a positive integer **X** which maximizes the expression **(A AND X) * (B AND X)**.

**Example:**

Input:A = 9 B = 8

Output:8

(9 AND 8) * (8 AND 8) = 8 * 8 = 64 (maximum possible)

Input:A = 11 and B = 13

Output:9

**Naive approach:** One can run a loop from **1** to **max(A, B)** and can easily find **X** which maximizes the given expression.

**Efficient approach:** It is known that,

(a – b)^{2}≥ 0

which implies(a + b)^{2}– 4*a*b ≥ 0

which impliesa * b ≤ (a + b)^{2}/ 4Hence, it concludes that

a * bwill be maximum whena * b = (a + b)^{2}/ 4

which impliesa = b

From the above result,(A AND X) * (B AND X)will be maximum when(A AND X) = (B AND X)

Now X can be found as:

A = 11 = 1011

B = 13 = 1101

X = ? = abcdAt 0th place: (1 AND d) = (1 AND d) implies d = 0, 1 but to maximize (A AND X) * (B AND X) d = 1

At 1st place: (1 AND d) = (0 AND d) implies c = 0

At 2nd place: (0 AND d) = (1 AND d) implies b = 0

At 3rd place: (1 AND d) = (1 AND d) implies a = 0, 1 but to maximize (A AND X) * (B AND X) a = 1Hence, X = 1001 = 9

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `#define MAX 32 ` ` ` `// Function to find X according ` `// to the given conditions ` `int` `findX(` `int` `A, ` `int` `B) ` `{ ` ` ` `int` `X = 0; ` ` ` ` ` `// int can have 32 bits ` ` ` `for` `(` `int` `bit = 0; bit < MAX; bit++) { ` ` ` ` ` `// Temporary ith bit ` ` ` `int` `tempBit = 1 << bit; ` ` ` ` ` `// Compute ith bit of X according to ` ` ` `// given conditions ` ` ` `// Expression below is the direct ` ` ` `// conclusion from the illustration ` ` ` `// we had taken earlier ` ` ` `int` `bitOfX = A & B & tempBit; ` ` ` ` ` `// Add the ith bit of X to X ` ` ` `X += bitOfX; ` ` ` `} ` ` ` ` ` `return` `X; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `A = 11, B = 13; ` ` ` ` ` `cout << findX(A, B); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` `static` `int` `MAX = ` `32` `; ` ` ` `// Function to find X according ` `// to the given conditions ` `static` `int` `findX(` `int` `A, ` `int` `B) ` `{ ` ` ` `int` `X = ` `0` `; ` ` ` ` ` `// int can have 32 bits ` ` ` `for` `(` `int` `bit = ` `0` `; bit < MAX; bit++) ` ` ` `{ ` ` ` ` ` `// Temporary ith bit ` ` ` `int` `tempBit = ` `1` `<< bit; ` ` ` ` ` `// Compute ith bit of X according to ` ` ` `// given conditions ` ` ` `// Expression below is the direct ` ` ` `// conclusion from the illustration ` ` ` `// we had taken earlier ` ` ` `int` `bitOfX = A & B & tempBit; ` ` ` ` ` `// Add the ith bit of X to X ` ` ` `X += bitOfX; ` ` ` `} ` ` ` `return` `X; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String []args) ` `{ ` ` ` `int` `A = ` `11` `, B = ` `13` `; ` ` ` ` ` `System.out.println(findX(A, B)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `MAX` `=` `32` ` ` `# Function to find X according ` `# to the given conditions ` `def` `findX(A, B) : ` ` ` ` ` `X ` `=` `0` `; ` ` ` ` ` `# int can have 32 bits ` ` ` `for` `bit ` `in` `range` `(` `MAX` `) : ` ` ` ` ` `# Temporary ith bit ` ` ` `tempBit ` `=` `1` `<< bit; ` ` ` ` ` `# Compute ith bit of X according to ` ` ` `# given conditions ` ` ` `# Expression below is the direct ` ` ` `# conclusion from the illustration ` ` ` `# we had taken earlier ` ` ` `bitOfX ` `=` `A & B & tempBit; ` ` ` ` ` `# Add the ith bit of X to X ` ` ` `X ` `+` `=` `bitOfX; ` ` ` ` ` `return` `X; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `A ` `=` `11` `; B ` `=` `13` `; ` ` ` `print` `(findX(A, B)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` `static` `int` `MAX = 32; ` ` ` `// Function to find X according ` `// to the given conditions ` `static` `int` `findX(` `int` `A, ` `int` `B) ` `{ ` ` ` `int` `X = 0; ` ` ` ` ` `// int can have 32 bits ` ` ` `for` `(` `int` `bit = 0; bit < MAX; bit++) ` ` ` `{ ` ` ` ` ` `// Temporary ith bit ` ` ` `int` `tempBit = 1 << bit; ` ` ` ` ` `// Compute ith bit of X according to ` ` ` `// given conditions ` ` ` `// Expression below is the direct ` ` ` `// conclusion from the illustration ` ` ` `// we had taken earlier ` ` ` `int` `bitOfX = A & B & tempBit; ` ` ` ` ` `// Add the ith bit of X to X ` ` ` `X += bitOfX; ` ` ` `} ` ` ` `return` `X; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` `int` `A = 11, B = 13; ` ` ` ` ` `Console.WriteLine(findX(A, B)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

9

## Recommended Posts:

- Maximize the Expression | Bit Manipulation
- Bit manipulation | Swap Endianness of a number
- Winner in the Rock-Paper-Scissor game using Bit manipulation
- Fast Exponention using Bit Manipulation
- Maximize the value of the given expression
- Bits manipulation (Important tactics)
- Inserting m into n such that m starts at bit j and ends at bit i.
- Inserting M into N such that m starts at bit j and ends at bit i | Set-2
- Count of 1-bit and 2-bit characters in the given binary string
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Maximum and Minimum Values of an Algebraic Expression
- What is an Expression and What are the types of Expressions?
- Expression Evaluation
- Find all possible outcomes of a given expression
- Check for balanced parentheses in an expression | O(1) space | O(N^2) time complexity
- Deriving the expression of Fibonacci Numbers in terms of golden ratio
- Program to evaluate the expression (√X+1)^6 + (√X-1)^6
- Find a permutation of 2N numbers such that the result of given expression is exactly 2K
- Print the balanced bracket expression using given brackets
- Find the minimum value of X for an expression

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.