📌  相关文章
📜  查找一个数字,使得XOR后数组中的最大值为最小值

📅  最后修改于: 2021-05-25 02:56:25             🧑  作者: Mango

给定非负整数数组。选择一个整数P ,然后对数组的所有元素进行P的XOR运算。任务是选择P ,以便在对数组的所有元素与P进行XOR之后,使数组的最大值最小。

例子:

方法:

  • 我们可以从最高有效位开始递归解决此问题。
  • 将元素分成两组,一组将当前位为开的元素设置为(1),另一组将当前位为开的元素设置为(0)。
  • 如果任一组为空,我们可以相应地分配P的当前位,以使答案中的当前位关闭。
  • 否则,如果两个组都不为空,那么无论我们为P的当前位分配什么值,我们都将在答案中将此位设置为on(1)。
  • 现在,要确定将哪个值分配给P的当前位,我们将递归调用下一组的每个组的下一个位,并返回两者的最小值。

下面是上述方法的实现:

C++
// C++ program that find the minimum
// possible maximum
#include 
using namespace std;
 
// Recursive function that find the
//  minimum value after exclusive-OR
int RecursiveFunction(vector ref,
                      int bit)
{
    // Condition if ref size is zero or
    // bit is negative then return 0
    if (ref.size() == 0 || bit < 0)
        return 0;
 
    vector curr_on, curr_off;
 
    for (int i = 0; i < ref.size(); i++)
    {
        // Condition if current bit is
        // off then push current value
        // in curr_off vector
        if (((ref[i] >> bit) & 1) == 0)
            curr_off.push_back(ref[i]);
         
        // Condition if current bit is on
        // then push current value in
        // curr_on vector
        else
            curr_on.push_back(ref[i]);
    }
 
    // Condition if curr_off is empty
    // then call recursive function
    // on curr_on vector
    if (curr_off.size() == 0)
        return RecursiveFunction(curr_on,
                                 bit - 1);
 
    // Condition if curr_on is empty
    // then call recursive function
    // on curr_off vector
    if (curr_on.size() == 0)
        return RecursiveFunction(curr_off,
                                 bit - 1);
 
    // Return the minimum of curr_off and 
    // curr_on and add power of 2 of
    // current bit
    return min(RecursiveFunction(curr_off,
                                 bit - 1),
               RecursiveFunction(curr_on,
                                 bit - 1))
           + (1 << bit);
}
 
// Function that print the minimum
// value after exclusive-OR
void PrintMinimum(int a[], int n)
{
    vector v;
 
    // Pushing values in vector
    for (int i = 0; i < n; i++)
        v.push_back(a[i]);
 
    // Printing answer
    cout << RecursiveFunction(v, 30)
         << "\n";
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 2, 1 };
 
    int size = sizeof(arr) / sizeof(arr[0]);
 
    PrintMinimum(arr, size);
 
    return 0;
}


Java
// Java program that find the minimum
// possible maximum
import java.util.*;
 
class GFG{
 
// Recursive function that find the
// minimum value after exclusive-OR
static int RecursiveFunction(ArrayList ref,
                             int bit)
{
     
    // Condition if ref size is zero or
    // bit is negative then return 0
    if (ref.size() == 0 || bit < 0)
        return 0;
 
    ArrayList curr_on = new ArrayList<>();
    ArrayList curr_off = new ArrayList<>();
 
    for(int i = 0; i < ref.size(); i++)
    {
         
        // Condition if current bit is
        // off then push current value
        // in curr_off vector
        if (((ref.get(i) >> bit) & 1) == 0)
            curr_off.add(ref.get(i));
 
        // Condition if current bit is on
        // then push current value in
        // curr_on vector
        else
            curr_on.add(ref.get(i));
    }
 
    // Condition if curr_off is empty
    // then call recursive function
    // on curr_on vector
    if (curr_off.size() == 0)
        return RecursiveFunction(curr_on, bit - 1);
 
    // Condition if curr_on is empty
    // then call recursive function
    // on curr_off vector
    if (curr_on.size() == 0)
        return RecursiveFunction(curr_off, bit - 1);
 
    // Return the minimum of curr_off and
    // curr_on and add power of 2 of
    // current bit
    return Math.min(RecursiveFunction(curr_off,
                                      bit - 1),
                    RecursiveFunction(curr_on,
                                      bit - 1)) +
                                     (1 << bit);
}
 
// Function that print the minimum
// value after exclusive-OR
static void PrintMinimum(int a[], int n)
{
    ArrayList v = new ArrayList<>();
 
    // Pushing values in vector
    for(int i = 0; i < n; i++)
        v.add(a[i]);
 
    // Printing answer
    System.out.println(RecursiveFunction(v, 30));
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 3, 2, 1 };
    int size = arr.length;
 
    PrintMinimum(arr, size);
}
}
 
// This code is contributed by jrishabh99


Python3
# Python3 program that find
# the minimum  possible maximum
 
# Recursive function that find the
#  minimum value after exclusive-OR
def RecursiveFunction(ref, bit):
 
    # Condition if ref size is zero or
    # bit is negative then return 0
    if(len(ref) == 0 or bit < 0):
        return 0;
    curr_on = []
    curr_off = []
 
    for i in range(len(ref)):
       
        # Condition if current bit is 
        # off then push current value 
        # in curr_off vector
        if(((ref[i] >> bit) & 1) == 0):
            curr_off.append(ref[i])
 
        # Condition if current bit is on 
        # then push current value in 
        # curr_on vector
        else:
            curr_on.append(ref[i])
 
    # Condition if curr_off is empty
    # then call recursive function
    # on curr_on vector
    if(len(curr_off) == 0):
        return RecursiveFunction(curr_on,
                                 bit - 1)
 
    # Condition if curr_on is empty
    # then call recursive function
    # on curr_off vector
    if(len(curr_on) == 0):
        return RecursiveFunction(curr_off,
                                 bit - 1)
 
    # Return the minimum of curr_off and  
    # curr_on and add power of 2 of
    # current bit
    return(min(RecursiveFunction(curr_off,
                                 bit - 1),
               RecursiveFunction(curr_on,
                                 bit - 1)) + (1 << bit))
 
# Function that print the minimum
# value after exclusive-OR
def PrintMinimum(a, n):
    v = []
 
    # Pushing values in vector
    for i in range(n):
        v.append(a[i])
 
    # Printing answer
    print(RecursiveFunction(v, 30))
 
# Driver Code
arr = [3, 2, 1]
size = len(arr)
PrintMinimum(arr, size)
 
#This code is contributed by avanitrachhadiya2155


C#
// C# program that find the minimum
// possible maximum
using System;
using System.Collections.Generic;
 
class GFG{
 
// Recursive function that find the
// minimum value after exclusive-OR
static int RecursiveFunction(List re,
                             int bit)
{
     
    // Condition if ref size is zero or
    // bit is negative then return 0
    if (re.Count == 0 || bit < 0)
        return 0;
 
    List curr_on = new List();
    List curr_off = new List();
 
    for(int i = 0; i < re.Count; i++)
    {
         
        // Condition if current bit is
        // off then push current value
        // in curr_off vector
        if (((re[i] >> bit) & 1) == 0)
            curr_off.Add(re[i]);
 
        // Condition if current bit is on
        // then push current value in
        // curr_on vector
        else
            curr_on.Add(re[i]);
    }
 
    // Condition if curr_off is empty
    // then call recursive function
    // on curr_on vector
    if (curr_off.Count == 0)
        return RecursiveFunction(curr_on,
                                 bit - 1);
 
    // Condition if curr_on is empty
    // then call recursive function
    // on curr_off vector
    if (curr_on.Count == 0)
        return RecursiveFunction(curr_off,
                                 bit - 1);
 
    // Return the minimum of curr_off and
    // curr_on and add power of 2 of
    // current bit
    return Math.Min(RecursiveFunction(curr_off,
                                      bit - 1),
                    RecursiveFunction(curr_on,
                                      bit - 1)) +
                                     (1 << bit);
}
 
// Function that print the minimum
// value after exclusive-OR
static void PrintMinimum(int []a, int n)
{
    List v = new List();
 
    // Pushing values in vector
    for(int i = 0; i < n; i++)
        v.Add(a[i]);
 
    // Printing answer
    Console.WriteLine(RecursiveFunction(v, 30));
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 3, 2, 1 };
    int size = arr.Length;
 
    PrintMinimum(arr, size);
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
2