📌  相关文章
📜  最小化 K,其与给定数组元素的异或使数组保持不变

📅  最后修改于: 2021-09-07 05:21:26             🧑  作者: Mango

给定一个包含N 个元素的数组,任务是找到K的最小值,使得K与所有数组元素的按位异或产生相同的元素集。如果无法找到K 的任何值,则打印“-1”

例子:

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

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

  1. 将所有元素插入到集合中。
  2. 迭代范围[0, 1024] 中所有可能的K值。
  3. 对于集合中的每个元素,找到它与K 的按位异或。
  4. K的第一个值,所有元素与元素 inset 进行按位异或后生成的所有元素都与给定集合的相同,然后打印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


Javascript


输出:
1

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live