给定一个由N个非负整数组成的数组A ,任务是选择一个整数K ,以使所有数组元素的K异或值的最大值最小。换句话说,对于某些K值,找到Z的最小可能值,其中Z = max(A [i] xor K) ,0 <= i <= n-1。
例子:
Input: A = [1, 2, 3]
Output: 2
Explanation:
On choosing K = 3, max(A[i] xor 3) = 2, and this is the minimum possible value.
Input: A = [3, 2, 5, 6]
Output: 5
方法:为了解决上述问题,我们将使用递归。我们将从递归函数的最高有效位开始。
- 在递归步骤中,将元素分为两部分-一个部分打开当前位,另一部分关闭当前位。如果任何部分中没有单个元素,则可以选择K的特定位,以使最终的异或值在该位位置处为0(因为我们的目标是使该值最小化),然后继续进行下一个操作。位在下一个递归步骤中。
- 如果两个部分都包含一些元素,则通过在该位位置放置0和1并在下一个递归调用中使用相应的部分来计算答案,来探索这两种可能性。
让answer_on是值,如果1被放置,并且如果0被放置在该位置(POS)answer_off是的值。由于两个部分都不为空,因此无论我们为K选择哪一位,都将2 pos加到最终值上。
对于每个递归步骤:
answer = min(answer_on, answer_off) + 2pos
下面是上述方法的实现:
C++
// C++ implementation to find Minimum
// possible value of the maximum xor
// in an array by choosing some integer
#include
using namespace std;
// Function to calculate Minimum possible
// value of the Maximum XOR in an array
int calculate(vector& section, int pos)
{
// base case
if (pos < 0)
return 0;
// Divide elements into two sections
vector on_section, off_section;
// Traverse all elements of current
// section and divide in two groups
for (auto el : section) {
if (((el >> pos) & 1) == 0)
off_section.push_back(el);
else
on_section.push_back(el);
}
// Check if one of the sections is empty
if (off_section.size() == 0)
return calculate(on_section, pos - 1);
if (on_section.size() == 0)
return calculate(off_section, pos - 1);
// explore both the possibilities using recursion
return min(calculate(off_section, pos - 1),
calculate(on_section, pos - 1))
+ (1 << pos);
}
// Function to calculate minimum XOR value
int minXorValue(int a[], int n)
{
vector section;
for (int i = 0; i < n; i++)
section.push_back(a[i]);
// Start recursion from the
// most significant pos position
return calculate(section, 30);
}
// Driver code
int main()
{
int N = 4;
int A[N] = { 3, 2, 5, 6 };
cout << minXorValue(A, N);
return 0;
}
Java
// Java implementation to find Minimum
// possible value of the maximum xor
// in an array by choosing some integer
import java.util.*;
class GFG{
// Function to calculate Minimum possible
// value of the Maximum XOR in an array
static int calculate(Vector section, int pos)
{
// Base case
if (pos < 0)
return 0;
// Divide elements into two sections
Vector on_section = new Vector(),
off_section = new Vector();
// Traverse all elements of current
// section and divide in two groups
for(int el : section)
{
if (((el >> pos) & 1) == 0)
off_section.add(el);
else
on_section.add(el);
}
// Check if one of the sections is empty
if (off_section.size() == 0)
return calculate(on_section, pos - 1);
if (on_section.size() == 0)
return calculate(off_section, pos - 1);
// Explore both the possibilities using recursion
return Math.min(calculate(off_section, pos - 1),
calculate(on_section, pos - 1)) +
(1 << pos);
}
// Function to calculate minimum XOR value
static int minXorValue(int a[], int n)
{
Vector section = new Vector();
for(int i = 0; i < n; i++)
section.add(a[i]);
// Start recursion from the
// most significant pos position
return calculate(section, 30);
}
// Driver code
public static void main(String[] args)
{
int N = 4;
int A[] = { 3, 2, 5, 6 };
System.out.print(minXorValue(A, N));
}
}
// This code is contributed by Princi Singh
Python3
# Python3 implementation to find Minimum
# possible value of the maximum xor
# in an array by choosing some integer
# Function to calculate Minimum possible
# value of the Maximum XOR in an array
def calculate(section, pos):
# base case
if (pos < 0):
return 0
# Divide elements into two sections
on_section = []
off_section = []
# Traverse all elements of current
# section and divide in two groups
for el in section:
if (((el >> pos) & 1) == 0):
off_section.append(el)
else:
on_section.append(el)
# Check if one of the sections is empty
if (len(off_section) == 0):
return calculate(on_section, pos - 1)
if (len(on_section) == 0):
return calculate(off_section, pos - 1)
# explore both the possibilities using recursion
return min(calculate(off_section, pos - 1),
calculate(on_section, pos - 1))+ (1 << pos)
# Function to calculate minimum XOR value
def minXorValue(a, n):
section = []
for i in range( n):
section.append(a[i]);
# Start recursion from the
# most significant pos position
return calculate(section, 30)
# Driver code
if __name__ == "__main__":
N = 4
A = [ 3, 2, 5, 6 ]
print(minXorValue(A, N))
# This code is contributed by chitranayal
C#
// C# implementation to find minimum
// possible value of the maximum xor
// in an array by choosing some integer
using System;
using System.Collections.Generic;
class GFG{
// Function to calculate minimum possible
// value of the maximum XOR in an array
static int calculate(List section, int pos)
{
// Base case
if (pos < 0)
return 0;
// Divide elements into two sections
List on_section = new List(),
off_section = new List();
// Traverse all elements of current
// section and divide in two groups
foreach(int el in section)
{
if (((el >> pos) & 1) == 0)
off_section.Add(el);
else
on_section.Add(el);
}
// Check if one of the sections is empty
if (off_section.Count == 0)
return calculate(on_section, pos - 1);
if (on_section.Count == 0)
return calculate(off_section, pos - 1);
// Explore both the possibilities using recursion
return Math.Min(calculate(off_section, pos - 1),
calculate(on_section, pos - 1)) +
(1 << pos);
}
// Function to calculate minimum XOR value
static int minXorValue(int []a, int n)
{
List section = new List();
for(int i = 0; i < n; i++)
section.Add(a[i]);
// Start recursion from the
// most significant pos position
return calculate(section, 30);
}
// Driver code
public static void Main(String[] args)
{
int N = 4;
int []A = { 3, 2, 5, 6 };
Console.Write(minXorValue(A, N));
}
}
// This code is contributed by Princi Singh
输出:
5
时间复杂度: O(N * log(max(A i ))