Open In App

Maximize Bitwise AND product function

Last Updated : 26 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++

// 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;
}

                    

Java

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

                    

Python3

# 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#

// 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

// 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: O(max(A, B))
Auxiliary Space: O(1)

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++

// 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

// 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

# 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)

                    

C#

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

// 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: O(1)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads