Maximum XOR pair product with a given value
Last Updated :
22 Aug, 2023
Given a positive integer value V, the task is to find the maximum XOR of two numbers such that their product is equal to the given value V.
Examples:
Input: V = 20
Output: 7
Explanation: For the given value of 20, there are several pairs of numbers whose product is 20: (1, 20), (2, 10), and (4, 5). The maximum XOR among these pairs is 7, which corresponds to the pair (1, 20).
Input: V = 36
Output: 39
Explanation: For the given value of 36, there are several pairs of numbers whose product is 36: (1, 36), (2, 18), (3, 12), (4, 9), and (6, 6). The maximum XOR among these pairs is 39, which corresponds to the pair (3, 12).
Approach: This can be solved with the following idea:
The approach takes advantage of the fact that if a and b are two numbers such that a * b = value, then the XOR of a and b will be maximum when a and b are as close to each other as possible. By iterating through the numbers up to the square root of the given value, we cover all possible pairs of factors and find the maximum XOR among them.
Below is the code for the above approach:
C++
#include <cmath>
#include <iostream>
int find_max_xor_product( int value)
{
int max_xor = 0;
int sqrt_value = sqrt (value);
for ( int num = 1; num <= sqrt_value; ++num) {
if (value % num == 0) {
int div = value / num;
max_xor = std::max(max_xor, num ^ div );
}
}
return max_xor;
}
int main()
{
int value = 36;
int max_xor = find_max_xor_product(value);
std::cout << max_xor << std::endl;
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static int findMaxXorProduct( int value)
{
int max_xor = 0 ;
int sqrt_value = ( int )Math.sqrt(value);
for ( int num = 1 ; num <= sqrt_value; ++num) {
if (value % num == 0 ) {
int div = value / num;
max_xor = Math.max(max_xor, num ^ div);
}
}
return max_xor;
}
public static void main(String[] args)
{
int value = 36 ;
int max_xor = findMaxXorProduct(value);
System.out.println(max_xor);
}
}
|
Python3
import math
def find_max_xor_product(value):
max_xor = 0
sqrt_value = int (math.sqrt(value))
for num in range ( 1 , sqrt_value + 1 ):
if value % num = = 0 :
div = value / / num
max_xor = max (max_xor, num ^ div)
return max_xor
if __name__ = = '__main__' :
value = 36
max_xor = find_max_xor_product(value)
print (max_xor)
|
C#
using System;
public class GFG {
static int FindMaxXorProduct( int value)
{
int max_xor = 0;
int sqrt_value = ( int )Math.Sqrt(value);
for ( int num = 1; num <= sqrt_value; ++num) {
if (value % num == 0) {
int div = value / num;
max_xor = Math.Max(max_xor, num ^ div);
}
}
return max_xor;
}
static void Main( string [] args)
{
int value = 36;
int max_xor = FindMaxXorProduct(value);
Console.WriteLine(max_xor);
}
}
|
Javascript
function find_max_xor_product(value) {
let max_xor = 0;
let sqrt_value = Math.sqrt(value);
for (let num = 1; num <= sqrt_value; ++num) {
if (value % num === 0) {
let div = value / num;
max_xor = Math.max(max_xor, num ^ div);
}
}
return max_xor;
}
let value = 36;
let max_xor = find_max_xor_product(value);
console.log(max_xor);
|
Time complexity: O(sqrt(N))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...