Maximize the Expression | Bit Manipulation
Given two positive integers A and B. Let’s define D such that B AND D = D. The task is to maximize the expression A XOR D.
Examples:
Input: A = 11 B = 4
Output: 15
Take D = 4 as (B AND D) = (4 AND 4) = 4.
Also, (A XOR D) = (11 XOR 4) = 15 which is the
maximum according to the given condition.
Input: A = 9 and B = 13
Output: 13
Naive approach: Since B AND D = D, D will always be smaller than or equal to B. Hence, one can run a loop from 1 to B and check whether the given conditions are satisfied or not.
Efficient approach: Instead of running a loop and checking for each D, the maximum value of the expression (A XOR D) can be easily calculated using Bit Manipulation techniques.
Let’s take an example to understand the way to approach the problem:
A = 11 = 1011, B = 14 = 1110
Let's assume D = abcd in base 2 notation
B AND D: 1110 A XOR D: 1011
abcd abcd
------ ------
abcd ????
At 0th place: (0 AND d) = d implies d = 0
At 1st place: (1 AND c) = c implies c = 0, 1 but to maximize (A XOR D), take c = 0
At 2nd place: (1 AND b) = b implies b = 0, 1 but to maximize (A XOR D), take b = 1
At 3rd place: (1 AND a) = a implies a = 0, 1 but to maximize (A XOR D), take a = 0
Hence, D = 0100 = 4 and maximum value of (A XOR D) = (11 XOR 4) = 15.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
#define MAX 32
int maximizeExpression( int a, int b)
{
int result = a;
for ( int bit = MAX - 1; bit >= 0; bit--) {
int bitOfD = 1 << bit;
int x = b & bitOfD;
if (x == bitOfD) {
int y = result & bitOfD;
if (y == 0) {
result = result ^ bitOfD;
}
}
}
return result;
}
int main()
{
int a = 11, b = 14;
cout << maximizeExpression(a, b);
return 0;
}
|
Java
class GFG
{
final static int MAX = 32 ;
static int maximizeExpression( int a, int b)
{
int result = a;
for ( int bit = MAX - 1 ; bit >= 0 ; bit--)
{
int bitOfD = 1 << bit;
int x = b & bitOfD;
if (x == bitOfD) {
int y = result & bitOfD;
if (y == 0 )
{
result = result ^ bitOfD;
}
}
}
return result;
}
public static void main (String[] args)
{
int a = 11 , b = 14 ;
System.out.println(maximizeExpression(a, b));
}
}
|
Python3
MAX = 32
def maximizeExpression(a, b) :
result = a
for bit in range ( MAX - 1 , - 1 , - 1 ) :
bitOfD = 1 << bit
x = b & bitOfD
if (x = = bitOfD) :
y = result & bitOfD
if (y = = 0 ) :
result = result ^ bitOfD
return result
a = 11
b = 14
print (maximizeExpression(a, b))
|
C#
using System;
class GFG
{
static int MAX = 32;
static int maximizeExpression( int a, int b)
{
int result = a;
for ( int bit = MAX - 1; bit >= 0; bit--)
{
int bitOfD = 1 << bit;
int x = b & bitOfD;
if (x == bitOfD)
{
int y = result & bitOfD;
if (y == 0)
{
result = result ^ bitOfD;
}
}
}
return result;
}
public static void Main (String []args)
{
int a = 11, b = 14;
Console.WriteLine(maximizeExpression(a, b));
}
}
|
Javascript
<script>
let MAX = 32;
function maximizeExpression(a, b)
{
let result = a;
for (let bit = MAX - 1; bit >= 0; bit--)
{
let bitOfD = 1 << bit;
let x = b & bitOfD;
if (x == bitOfD) {
let y = result & bitOfD;
if (y == 0) {
result = result ^ bitOfD;
}
}
}
return result;
}
let a = 11, b = 14;
document.write(maximizeExpression(a, b));
</script>
|
Time Complexity: O(MAX)
Auxiliary Space: O(1)
Last Updated :
10 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...