📌  相关文章
📜  最小化与给定数组元素进行XOR运算的K

📅  最后修改于: 2021-05-25 05:35:23             🧑  作者: Mango

给定N个元素的数组,任务是找到K的最小值,以使K与所有数组元素的按位XOR运算产生相同的元素集。如果找不到任何K值,则打印“ -1”

例子:

幼稚的方法:幼稚的方法是对K[1,1024]范围内的所有可能值进行迭代,并检查K与数组中所有元素的按位XOR是否提供相同的数组元素。如果对于K的任何最小值,按位XOR生成相同的数组,则打印该K值,否则打印“ -1”
时间复杂度: O(K * N 2 )
辅助空间: O(1)

高效的方法:可以通过使用额外的空间来优化上述方法。步骤如下:

  1. 将所有元素插入集合。
  2. [0,1024]范围内的K的所有可能值进行迭代。
  3. 对于集合中的每个元素,找到与K的按位XOR。
  4. 按位XOR之后与集合中的元素生成的所有元素的K的第一个值与给定集合的值相同,然后打印K的值。
  5. 如果没有获得这样的K ,则打印“ -1”

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum
// value of K in given range
int min_value(int arr[], int N)
{
    int x, X, K;
 
    // Declare a set
    set S;
 
    for (int i = 0; i < N; i++) {
        S.insert(arr[i]);
    }
 
    // Initialize count variable
    int count = 0;
 
    // Iterate in range [1, 1024]
    for (int i = 1; i <= 1024; i++) {
 
        // counter set as 0
        count = 0;
 
        // Iterating through the Set
        for (auto it = S.begin(); it != S.end(); it++)
 
        // Check if the XOR
        // calculated is present
        // in the Set
        {
 
            X = ((i | *it) - (i & *it));
 
            // If the value of Bitwise XOR
            // inside the given set then
            // increment count
            if (S.find(X) != S.end()) {
                count++;
            }
        }
 
        // Check if the value of count is
        // equal to the size of set
        if (count == S.size()) {
            K = i;
 
            // Return minimum value of K
            return K;
        }
    }
 
    // If no such K is found
    return -1;
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 1, 0, 3, 3, 0, 2 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << min_value(arr, N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG {
 
    // Function to find the minimum
    // value of K in given range
    static int min_value(int arr[], int N)
    {
        int x, X, K;
 
        // Declare a set
        HashSet S = new HashSet();
 
        for (int i = 0; i < N; i++) {
            S.add(arr[i]);
        }
 
        // Initialize count variable
        int count = 0;
 
        // Iterate in range [1, 1024]
        for (int i = 1; i <= 1024; i++) {
 
            // counter set as 0
            count = 0;
 
            // Iterating through the Set
            for (int it : S) {
 
                // Check if the XOR
                // calculated is present
                // in the Set
                X = ((i | it) - (i & it));
 
                // If the value of Bitwise XOR
                // inside the given set then
                // increment count
                if (S.contains(X)) {
                    count++;
                }
            }
 
            // Check if the value of count is
            // equal to the size of set
            if (count == S.size()) {
                K = i;
 
                // Return minimum value of K
                return K;
            }
        }
 
        // If no such K is found
        return -1;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // Given array
        int arr[] = { 1, 0, 3, 3, 0, 2 };
 
        int N = arr.length;
 
        // Function Call
        System.out.print(min_value(arr, N));
    }
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program for the above approach
 
# Function to find the minimum
# value of K in given range
 
 
def min_value(arr, N):
 
    x, X, K = 0, 0, 0
 
    # Declare a set
    S = set()
 
    for i in range(N):
        S.add(arr[i])
 
    # Initialize count variable
    count = 0
 
    # Iterate in range [1, 1024]
    for i in range(1, 1024):
 
        # counter set as 0
        count = 0
 
        # Iterating through the Set
        for it in S:
 
            # Check if the XOR
            # calculated is present
            # in the Set
            X = ((i | it) - (i & it))
 
            # If the value of Bitwise XOR
            # inside the given set then
            # increment count
            if X in S:
                count += 1
 
        # Check if the value of count is
        # equal to the size of set
        if (count == len(S)):
            K = i
 
            # Return minimum value of K
            return K
 
    # If no such K is found
    return -1
 
# Driver Code
 
 
# Given array
arr = [1, 0, 3, 3, 0, 2]
 
N = len(arr)
 
# Function Call
print(min_value(arr, N))
 
# This code is contributed by shubhamsingh10


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG {
 
    // Function to find the minimum
    // value of K in given range
    static int min_value(int[] arr, int N)
    {
        // int x;
        int X, K;
 
        // Declare a set
        HashSet S = new HashSet();
 
        for (int i = 0; i < N; i++) {
            S.Add(arr[i]);
        }
 
        // Initialize count variable
        int count = 0;
 
        // Iterate in range [1, 1024]
        for (int i = 1; i <= 1024; i++) {
 
            // counter set as 0
            count = 0;
 
            // Iterating through the Set
            foreach(int it in S)
            {
 
                // Check if the XOR
                // calculated is present
                // in the Set
                X = ((i | it) - (i & it));
 
                // If the value of Bitwise XOR
                // inside the given set then
                // increment count
                if (S.Contains(X)) {
                    count++;
                }
            }
 
            // Check if the value of count is
            // equal to the size of set
            if (count == S.Count) {
                K = i;
 
                // Return minimum value of K
                return K;
            }
        }
 
        // If no such K is found
        return -1;
    }
 
    // Driver code
    static void Main()
    {
 
        // Given array
        int[] arr = { 1, 0, 3, 3, 0, 2 };
 
        int N = arr.Length;
 
        // Function call
        Console.Write(min_value(arr, N));
    }
}
 
// This code is contributed by divyeshrabadiya07


输出:
1


时间复杂度: O(K * N * log 2 N)
辅助空间: O(1)