Maximize Bitwise AND product function
Last Updated :
26 Sep, 2023
Given two non-negative integers A and B find the smallest non-negative integer m such that the product of (A & m) and (B & m) is maximized.
Examples:
Input: A = 5, B = 10
Output: 15
Explanation: By taking m = 15 we got the maximum value of 50. We can also get 50 by other integers but 15 is the smallest to give maximum value.
Input: A = 10000000, B = 64235678983
Output: 64235683719
Naive Approach: To solve the problem follow the below idea:
We can iterate through all the possible numbers from 0 to 2 times the maximum of A and B and store the maximum value. For each maximum value update the value of m and return the answer.
Below is the code for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long countValue( long long A, long long B)
{
long long m = 0, mx = 0;
for ( long long i = 0; i <= 2 * max(A, B); i++) {
long long val = (A & i) * (B & i);
if (val > mx) {
mx = val;
m = i;
}
}
return m;
}
int main()
{
long long A = 5, B = 10;
long long m = countValue(A, B);
cout << m << "\n" ;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
long A = 5 , B = 10 ;
long m = countValue(A, B);
System.out.println(m);
}
public static long countValue( long A, long B) {
long m = 0 , mx = 0 ;
for ( long i = 0 ; i <= 2 * Math.max(A, B); i++) {
long val = (A & i) * (B & i);
if (val > mx) {
mx = val;
m = i;
}
}
return m;
}
}
|
Python3
def countValue(A, B):
m = 0
mx = 0
for i in range ( 2 * max (A, B) + 1 ):
val = (A & i) * (B & i)
if (val > mx):
mx = val
m = i
return m
A = 5
B = 10
m = countValue(A, B)
print (m)
|
C#
using System;
public class GFG
{
public static void Main( string [] args)
{
long A = 5, B = 10;
long m = CountValue(A, B);
Console.WriteLine(m);
}
public static long CountValue( long A, long B)
{
long m = 0, mx = 0;
for ( long i = 0; i <= 2 * Math.Max(A, B); i++)
{
long val = (A & i) * (B & i);
if (val > mx)
{
mx = val;
m = i;
}
}
return m;
}
}
|
Javascript
function countValue(A, B) {
let m = 0;
let mx = 0;
for (let i = 0; i < 2 * Math.max(A, B) + 1; i++) {
let val = (A & i) * (B & i);
if (val > mx) {
mx = val;
m = i;
}
}
return m;
}
let A = 5;
let B = 10;
let m = countValue(A, B);
console.log(m);
|
Time Complexity:
Auxiliary Space:
Efficient Approach: To solve the problem follow the below observations:
The observation is that the integers can be large, bitwise manipulation should have some play in solving this problem. To maximize the product of two numbers we should make the two numbers as large as possible. AND of two numbers is largest when two numbers are the same i.e. the set bit locations are the same. We find m such that the set bits of both A and B are set. Logical OR sets the bit ON when both or any one of the bits is SET. Hence A OR B will give us the value of m. It will automatically be the minimum value as only these bits are set which are contributing to the answer. Using this approach we can solve the problem of large integers and also reduce the complexity to constant time.
Below are the steps for the above approach:
- Initialize two variables m = 0 that will store the smallest integer value such that the product of (A & m) and (B & m) is maximized.
- Perform logical OR operation on A and B and store the result in variable m.
- Return m.
Below is the code for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long countValue( long long A, long long B)
{
long long m = 0, mx = 0;
m = A | B;
return m;
}
int main()
{
long long A = 5, B = 10;
long long m = countValue(A, B);
cout << m << "\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static long countValue( long A, long B)
{
long m = 0 , mx = 0 ;
m = A | B;
return m;
}
public static void main(String[] args)
{
long A = 5 , B = 10 ;
long m = countValue(A, B);
System.out.println(m);
}
}
|
Python
def countValue(A, B):
m = 0
mx = 0
m = A | B
return m
if __name__ = = '__main__' :
A = 5
B = 10
m = countValue(A, B)
print (m)
|
C#
using System;
public class Program
{
public static long CountValue( long A, long B)
{
long m = 0;
m = A | B;
return m;
}
public static void Main( string [] args)
{
long A = 5, B = 10;
long m = CountValue(A, B);
Console.WriteLine(m);
}
}
|
Javascript
function countValue(A, B) {
let m = 0;
let mx = 0;
m = A | B;
return m;
}
const A = 5;
const B = 10;
const m = countValue(A, B);
console.log(m);
|
Time Complexity:
Auxiliary Space:
Share your thoughts in the comments
Please Login to comment...