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++
// C++ code for the above approach:
#include <cmath>
#include <iostream>
// Find maximum XOR pair value
int find_max_xor_product(int value)
{
int max_xor = 0;
// Calculate the square
// root of the given value
int sqrt_value = sqrt(value);
// Iterate from 1 to the square root
// of the value
for (int num = 1; num <= sqrt_value; ++num) {
// Check if num is a divisor
// of the value
if (value % num == 0) {
// Calculate the
// corresponding divisor
int div = value / num;
max_xor = std::max(max_xor, num ^ div);
}
}
// Return the maximum XOR value
return max_xor;
}
// Driver code
int main()
{
int value = 36;
// Function call
int max_xor = find_max_xor_product(value);
std::cout << max_xor << std::endl;
return 0;
}
Java
// Java code for the above approach
import java.util.*;
public class GFG {
// Find maximum XOR pair value
public static int findMaxXorProduct(int value)
{
int max_xor = 0;
// Calculate the square root of the given value
int sqrt_value = (int)Math.sqrt(value);
// Iterate from 1 to the square root of the value
for (int num = 1; num <= sqrt_value; ++num) {
// Check if num is a divisor of the value
if (value % num == 0) {
// Calculate the corresponding divisor
int div = value / num;
max_xor = Math.max(max_xor, num ^ div);
}
}
// Return the maximum XOR value
return max_xor;
}
public static void main(String[] args)
{
int value = 36;
// Function call
int max_xor = findMaxXorProduct(value);
System.out.println(max_xor);
}
}
// This code is contributed by Susobhan Akhuli
Python3
#Python code for the above approach:
import math
# Find maximum XOR pair value
def find_max_xor_product(value):
max_xor = 0
# Calculate the square root of the given value
sqrt_value = int(math.sqrt(value))
# Iterate from 1 to the square root of the value
for num in range(1, sqrt_value + 1):
# Check if num is a divisor of the value
if value % num == 0:
# Calculate the corresponding divisor
div = value // num
max_xor = max(max_xor, num ^ div)
# Return the maximum XOR value
return max_xor
# Driver code
if __name__ == '__main__':
value = 36
# Function call
max_xor = find_max_xor_product(value)
print(max_xor)
C#
// C# code for the above approach
using System;
public class GFG {
// Find maximum XOR pair value
static int FindMaxXorProduct(int value)
{
int max_xor = 0;
// Calculate the square root
// of the given value
int sqrt_value = (int)Math.Sqrt(value);
// Iterate from 1 to the square root
// of the value
for (int num = 1; num <= sqrt_value; ++num) {
// Check if num is a divisor
// of the value
if (value % num == 0) {
// Calculate the corresponding divisor
int div = value / num;
max_xor = Math.Max(max_xor, num ^ div);
}
}
// Return the maximum XOR value
return max_xor;
}
// Driver code
static void Main(string[] args)
{
int value = 36;
// Function call
int max_xor = FindMaxXorProduct(value);
Console.WriteLine(max_xor);
}
}
// This code is contributed by Susobhan Akhuli
JavaScript
// JavaScript code for the above approach
// Find maximum XOR pair value
function find_max_xor_product(value) {
let max_xor = 0;
// Calculate the square root of the given value
let sqrt_value = Math.sqrt(value);
// Iterate from 1 to the square root of the value
for (let num = 1; num <= sqrt_value; ++num) {
// Check if num is a divisor of the value
if (value % num === 0) {
// Calculate the corresponding divisor
let div = value / num;
max_xor = Math.max(max_xor, num ^ div);
}
}
// Return the maximum XOR value
return max_xor;
}
// Driver code
let value = 36;
// Function call
let max_xor = find_max_xor_product(value);
console.log(max_xor);
// This code is contributed by Susobhan Akhuli
Time complexity: O(sqrt(N))
Auxiliary Space: O(1)
Similar Reads
Value in a given range with maximum XOR Given positive integers N, L, and R, we have to find the maximum value of N ? X, where X ? [L, R].Examples: Input : N = 7 L = 2 R = 23 Output : 23 Explanation : When X = 16, we get 7 ? 16 = 23 which is the maximum value for all X ? [2, 23].Input : N = 10 L = 5 R = 12 Output : 15 Explanation : When X
10 min read
Find a pair from the given array with maximum nCr value Given an array arr[] of n positive integers. The task is to find elements arr[i] and arr[j] from the array such that arr[i]Carr[j] is maximum possible. In case of more than 1 valid pairs, print any one of them.Examples: Input: arr[] = {3, 1, 2} Output: 3 2 3C1 = 3 3C2 = 3 2C1 = 2 (3, 1) and (3, 2) a
14 min read
Queries to find maximum product pair in range with updates You are given an array arr[] of positive integers. Your task is to process q queries, each consisting of three integers, of the following two types:[1, L, R]: Find the maximum product of any two distinct elements in the subarray arr[Lâ¦R] (inclusive).[2, i, val]: Update the element at index i to val
15+ min read
Maximum XOR value of a pair from a range Given a range [L, R], we need to find two integers in this range such that their XOR is maximum among all possible choices of two integers. More Formally, given [L, R], find max (A ^ B) where L <= A, B Examples : Input : L = 8 R = 20 Output : 31 31 is XOR of 15 and 16. Input : L = 1 R = 3 Output
6 min read
Maximum product of any path in given Binary Tree Given a binary tree of N nodes, the task is to find the maximum product of the elements of any path in the binary tree. Note: A path starts from the root and ends at any leaf in the tree. Examples: Input: 4 / \ 2 8 / \ / \2 1 3 4 Output: 128Explanation: Path in the given tree goes like {4, 8, 4} whi
5 min read
Find a pair with maximum product in array of Integers Given an array with both +ive and -ive integers, return a pair with the highest product. Examples : Input: arr[] = {1, 4, 3, 6, 7, 0} Output: {6,7} Input: arr[] = {-1, -3, -4, 2, 0, -5} Output: {-4,-5} Recommended PracticeMaximum product of two numbersTry It! A Simple Solution is to consider every p
15+ min read