📜  在 [0, K] 范围内找到一个值 X,它可以在给定数组上最大化 X XOR 和

📅  最后修改于: 2022-05-13 01:56:04.721000             🧑  作者: Mango

在 [0, K] 范围内找到一个值 X,它可以在给定数组上最大化 X XOR 和

给定一个大小为N的数组a[]和一个整数K,任务是在[0, K]范围内找到一个值X ,它可以最大化给定函数的值

例子:

方法:想法是考虑K的每一个值,然后找到满足上述条件的X值。请按照以下步骤解决问题:

  • 将变量maxX初始化为0-1以存储定义的最大总和以及发生它的X的值。
  • 使用变量j迭代范围[0, K]并执行以下任务:
    • 将变量sum初始化为0以存储定义的总和。
    • 使用变量i遍历范围[0, N)并执行以下任务:
      • 将 sum 的值设置为sum + j^a[i]。
    • 如果sum大于max,则将max的值设置为sum ,将X设置为j。
  • 执行上述步骤后,打印X的值作为答案。

下面是上述方法的实现

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the value of X for which
// the given sum maximises
void findValue(vector& a, int N, int K)
{
 
    // Variables to store the maximum
    // sum and that particular value
    // of X.
    long long int max = 0, X = -1;
 
    // Check every value of K
    for (int j = 0; j <= K; j++) {
 
        // Variable to store the desired sum
        long long int sum = 0;
        for (int i = 0; i < N; i++) {
 
            (sum = sum + (j ^ a[i]));
        }
 
        // Check the condition
        if (sum > max) {
            max = sum;
            X = j;
        }
    }
 
    // Print the result
    cout << X;
}
 
// Driver Code
int main()
 
{
 
    vector a = { 1, 2, 3, 4, 5, 6 };
    int N = 6, K = 10;
 
    findValue(a, N, K);
 
    return 0;
}


C
// C program for the above approach
#include 
 
// Function to find the value of X for which
// the given sum maximises
void findValue(int *a, int N, int K)
{
 
    // Variables to store the maximum
    // sum and that particular value
    // of X.
    long long int max = 0, X = -1;
 
    // Check every value of K
    for (int j = 0; j <= K; j++) {
 
        // Variable to store the desired sum
        long long int sum = 0;
        for (int i = 0; i < N; i++) {
 
            (sum = sum + (j ^ a[i]));
        }
 
        // Check the condition
        if (sum > max) {
            max = sum;
            X = j;
        }
    }
 
    // Print the result
    printf("%lli", X);
}
 
// Driver Code
int main()
{
 
    int a[] = { 1, 2, 3, 4, 5, 6 };
    int N = 6, K = 10;
 
    findValue(a, N, K);
 
    return 0;
}
 
// This code is contributed by palashi.


Java
// Java program for the above approach
class GFG {
 
    // Function to find the value of X for which
    // the given sum maximises
    public static void findValue(int[] a, int N, int K) {
 
        // Variables to store the maximum
        // sum and that particular value
        // of X.
        int max = 0, X = -1;
 
        // Check every value of K
        for (int j = 0; j <= K; j++) {
 
            // Variable to store the desired sum
            int sum = 0;
            for (int i = 0; i < N; i++) {
 
                sum = sum + (j ^ a[i]);
            }
 
            // Check the condition
            if (sum > max) {
                max = sum;
                X = j;
            }
        }
 
        // Print the result
        System.out.println(X);
    }
 
    // Driver Code
    public static void main(String args[])
    {
        int[] a = { 1, 2, 3, 4, 5, 6 };
        int N = 6, K = 10;
 
        findValue(a, N, K);
 
    }
 
}
 
// This code is contributed by saurabh_jaiswal.


Python3
# Python3 program for the above approach
 
# Function to find the value of X for which
# the given sum maximises
def findValue(a, N, K) :
 
    # Variables to store the maximum
    # sum and that particular value
    # of X.
    max = 0; X = -1;
 
    # Check every value of K
    for j in range( K + 1) :
 
        # Variable to store the desired sum
        sum = 0;
        for i in range(N) :
 
            sum = sum + (j ^ a[i]);
 
        # Check the condition
        if (sum > max) :
            max = sum;
            X = j;
      
    # Print the result
    print(X);
 
# Driver Code
if __name__ == "__main__" :
 
    a = [ 1, 2, 3, 4, 5, 6 ];
    N = 6; K = 10;
 
    findValue(a, N, K);
 
    # This code is contributed by AnkThon


C#
// C# program for the above approach
using System;
 
public class GFG {
 
    // Function to find the value of X for which
    // the given sum maximises
    public static void findValue(int[] a, int N, int K) {
 
        // Variables to store the maximum
        // sum and that particular value
        // of X.
        int max = 0, X = -1;
 
        // Check every value of K
        for (int j = 0; j <= K; j++) {
 
            // Variable to store the desired sum
            int sum = 0;
            for (int i = 0; i < N; i++) {
 
                sum = sum + (j ^ a[i]);
            }
 
            // Check the condition
            if (sum > max) {
                max = sum;
                X = j;
            }
        }
 
        // Print the result
        Console.WriteLine(X);
    }
 
    // Driver Code
    public static void Main(string []args)
    {
        int[] a = { 1, 2, 3, 4, 5, 6 };
        int N = 6, K = 10;
 
        findValue(a, N, K);
    }
}
 
// This code is contributed by AnkThon


Javascript


输出
8

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