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++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to find the value of m long long countValue( long long A, long long B)
{ // Maintain to variables m and mx to
// find the smallest value and
// maximum funcion value
long long m = 0, mx = 0;
// Loop till 2 times as i can be
// in this interval only
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 the answer
return m;
} // Driver Code int main()
{ long long A = 5, B = 10;
// Function Call
long long m = countValue(A, B);
cout << m << "\n" ;
return 0;
} |
import java.util.*;
public class Main {
public static void main(String[] args) {
long A = 5 , B = 10 ;
// Function Call
long m = countValue(A, B);
System.out.println(m);
}
// Function to find the value of m
public static long countValue( long A, long B) {
// Maintain to variables m and mx to
// find the smallest value and
// maximum function value
long m = 0 , mx = 0 ;
// Loop till 2 times as i can be
// in this interval only
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 the answer
return m;
}
} // This code is contributed by Prajwal Kandekar |
# Python program for the above approach # Function to find the value of m def countValue(A, B):
# Maintain to variables m and mx to
# find the smallest value and
# maximum funcion value
m = 0
mx = 0
# Loop till 2 times as i can be
# in this interval only
for i in range ( 2 * max (A, B) + 1 ):
val = (A & i) * (B & i)
if (val > mx):
mx = val
m = i
# Return the answer
return m
# Driver Code A = 5
B = 10
# Function Call m = countValue(A, B)
print (m)
# This code is contributed by # Tapesh(tapeshdua420) |
// C# program for the above approach using System;
public class GFG
{ public static void Main( string [] args)
{
long A = 5, B = 10;
// Function Call
long m = CountValue(A, B);
Console.WriteLine(m);
}
// Function to find the value of m
public static long CountValue( long A, long B)
{
// Maintain two variables m and mx to
// find the smallest value and maximum function value
long m = 0, mx = 0;
// Loop till 2 times as i can be
// in this interval only
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 the answer
return m;
}
} |
// JavaScript program for the above approach // Function to find the value of m function countValue(A, B) {
// Maintain to variables m and mx to
// find the smallest value and
// maximum funcion value
let m = 0;
let mx = 0;
// Loop till 2 times as i can be
// in this interval only
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 the answer
return m;
} // Driver Code let A = 5; let B = 10; // Function Call let m = countValue(A, B); console.log(m); // This code is contributed by Tapesh(tapeshdua420) |
Output
15
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++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to find the value of m long long countValue( long long A, long long B)
{ // Maintain to variables m and mx
// to find the smallest value and
// maximum funcion value
long long m = 0, mx = 0;
// Logical OR sets the bit ON when
// both or any one of the bits
// is SET.
m = A | B;
// Return the answer
return m;
} // Driver Code int main()
{ long long A = 5, B = 10;
// Function Call
long long m = countValue(A, B);
cout << m << "\n" ;
return 0;
} |
// Java program for the above approach import java.util.*;
class GFG {
// Function to find the value of m
public static long countValue( long A, long B)
{
// Maintain to variables m and mx
// to find the smallest value and
// maximum function value
long m = 0 , mx = 0 ;
// Logical OR sets the bit ON when
// both or any one of the bits
// is SET.
m = A | B;
// Return the answer
return m;
}
// Driver Code
public static void main(String[] args)
{
long A = 5 , B = 10 ;
// Function Call
long m = countValue(A, B);
System.out.println(m);
}
} // This code is contributed by prasad264 |
# python program for the above approach # Function to find the value of m def countValue(A, B):
# Maintain two variables m and mx
# to find the smallest value and
# maximum function value
m = 0
mx = 0
# Logical OR sets the bit ON when
# both or any one of the bits
# is SET.
m = A | B
# Return the answer
return m
# Driver Code if __name__ = = '__main__' :
A = 5
B = 10
# Function Call
m = countValue(A, B)
print (m)
|
using System;
public class Program
{ // Function to find the value of m
public static long CountValue( long A, long B)
{
// Maintain to variables m
// to find the smallest value
long m = 0;
// Logical OR sets the bit ON when
// both or any one of the bits
// is SET.
m = A | B;
// Return the answer
return m;
}
// Driver Code
public static void Main( string [] args)
{
long A = 5, B = 10;
long m = CountValue(A, B);
Console.WriteLine(m);
}
} |
// Javascript code function countValue(A, B) {
// Maintain two variables, m and mx,
// to find the smallest value and maximum function value
let m = 0;
let mx = 0;
// Logical OR sets the bit ON when
// both or any one of the bits
// is SET.
m = A | B;
// Return the answer
return m;
} // Driver Code const A = 5; const B = 10; // Function Call const m = countValue(A, B); console.log(m); // This code is contributed by guptapratik |
Output
15
Time Complexity:
Auxiliary Space: