📌  相关文章
📜  通过分别在 [0, A], [0, B] 和 [0, C] 范围内选择 3 个数字来最大化 XOR

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

通过分别在 [0, A], [0, B] 和 [0, C] 范围内选择 3 个数字来最大化 XOR

给定 3 个整数A , B , C ,任务是分别从每个范围 [0, A], [0, B], [0, C] 中选择三个数的最大 XOR 值。

例子:

朴素方法:在上述范围内生成所有可能的三元组并计算可能的最大异或。

时间复杂度:O(A*B*C)

辅助空间:O(1)

有效的方法:

要找到 XOR 操作的最大值,XOR 的值应该让每个位都是一个集合位,即在 32 位数字中,目标是从左到右获得最多的 1 个集合。这可以通过以下步骤来完成:

  1. 创建一个变量ans ,它将存储可能的最大异或值并将其初始化为零。
  2. 要创建一个可能最大异或的 32 位值,请对其从最高有效位到最低有效位的每个位运行一个循环。
  3. 现在对于每一位:
    • 创建一个变量cur来存储设置该位的最小数字。
    • 检查每个范围 A、B、C 并尝试找到在该位置具有设置位的数字。这可以通过将 A、B 和 C 与 cur 进行比较来完成,即如果 A、B 和 C 中的任何数字大于或等于 cur,那么它将在该位置包含一个设置位。如果在 A、B、C 中的任何一个数字中找到设置位,则最大异或也将包含该位置的设置位。
    • 现在,如果找到设置位,则将 cur 添加到 ans 以在其中设置该位,并将找到设置位的数字的值(从 A、B、C 中)减少 cur 以取消设置该位为它不能再次使用。
    • 对每一位做上述步骤,计算出答案的最终值。
  4. 完成上述步骤后,打印所需的结果。

执行:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to calculate
// maximum triplet XOR form 3 ranges
int maximumTripletXOR(int A, int B, int C)
{
    // Initialize a variable
    // to store the answer
    int ans = 0;
    for (int i = 30; i >= 0; i--) {
 
        // create minimum number
        // that have a set bit
        // at ith position
        int cur = 1 << i;
 
        // Check for each number
        // and try to greedily choose
        // the bit if possible
        // If A >= cur then A also have a
        // set bit at ith position
        if (A >= cur) {
 
            // Increment the answer
            ans += cur;
 
            // Subtract this value from A
            A -= cur;
        }
 
        // Check for B
        else if (B >= cur) {
 
            // Increment the answer
            ans += cur;
 
            // Subtract this value from B
            B -= cur;
        }
 
        // Check for C
        else if (C >= cur) {
 
            // Increment the answer
            ans += cur;
 
            // Subtract this value from C
            C -= cur;
        }
 
        // If any of the above conditions
        // is not satisfied then
        // there is no way to turn that bit ON.
    }
    return ans;
}
 
// Driver Code
int main()
{
    int A = 6;
    int B = 2;
    int C = 10;
    cout << maximumTripletXOR(A, B, C);
}


Java
// Java Program to implement
// the above approach
 
import java.util.*;
 
class GFG{
 
// Function to calculate
// maximum triplet XOR form 3 ranges
static int maximumTripletXOR(int A, int B, int C)
{
   
    // Initialize a variable
    // to store the answer
    int ans = 0;
    for (int i = 30; i >= 0; i--) {
 
        // create minimum number
        // that have a set bit
        // at ith position
        int cur = 1 << i;
 
        // Check for each number
        // and try to greedily choose
        // the bit if possible
        // If A >= cur then A also have a
        // set bit at ith position
        if (A >= cur) {
 
            // Increment the answer
            ans += cur;
 
            // Subtract this value from A
            A -= cur;
        }
 
        // Check for B
        else if (B >= cur) {
 
            // Increment the answer
            ans += cur;
 
            // Subtract this value from B
            B -= cur;
        }
 
        // Check for C
        else if (C >= cur) {
 
            // Increment the answer
            ans += cur;
 
            // Subtract this value from C
            C -= cur;
        }
 
        // If any of the above conditions
        // is not satisfied then
        // there is no way to turn that bit ON.
    }
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int A = 6;
    int B = 2;
    int C = 10;
    System.out.print(maximumTripletXOR(A, B, C));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python Program to implement
# the above approach
 
# Function to calculate
# maximum triplet XOR form 3 ranges
def maximumTripletXOR(A, B, C):
     
    # Initialize a variable
    # to store the answer
    ans = 0
     
    for i in range(30, -1,-1):
         
        # create minimum number
        # that have a set bit
        # at ith position
        cur = 1 << i
         
        # Check for each number
        # and try to greedily choose
        # the bit if possible
        # If A >= cur then A also have a
        # set bit at ith position
        if (A >= cur):
             
            # Increment the answer
            ans += cur
             
            # Subtract this value from A
            A -= cur
        # Check for B
        elif (B >= cur):
             
            # Increment the answer
            ans += cur
             
            # Subtract this value from B
            B -= cur
         
        # Check for C
        elif (C >= cur):
            # Increment the answer
            ans += cur
             
            # Subtract this value from C
            C -= cur
         
        # If any of the above conditions
        # is not satisfied then
        # there is no way to turn that bit ON.
         
    return ans
     
# Driver Code
 
A = 6
B = 2
C = 10
print(maximumTripletXOR(A, B, C))
 
# This code is contributed by shivanisinghss2110


C#
// C# Program to implement
// the above approach
using System;
 
class GFG{
 
// Function to calculate
// maximum triplet XOR form 3 ranges
static int maximumTripletXOR(int A, int B, int C)
{
   
    // Initialize a variable
    // to store the answer
    int ans = 0;
    for (int i = 30; i >= 0; i--) {
 
        // create minimum number
        // that have a set bit
        // at ith position
        int cur = 1 << i;
 
        // Check for each number
        // and try to greedily choose
        // the bit if possible
        // If A >= cur then A also have a
        // set bit at ith position
        if (A >= cur) {
 
            // Increment the answer
            ans += cur;
 
            // Subtract this value from A
            A -= cur;
        }
 
        // Check for B
        else if (B >= cur) {
 
            // Increment the answer
            ans += cur;
 
            // Subtract this value from B
            B -= cur;
        }
 
        // Check for C
        else if (C >= cur) {
 
            // Increment the answer
            ans += cur;
 
            // Subtract this value from C
            C -= cur;
        }
 
        // If any of the above conditions
        // is not satisfied then
        // there is no way to turn that bit ON.
    }
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
    int A = 6;
    int B = 2;
    int C = 10;
    Console.Write(maximumTripletXOR(A, B, C));
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出
15

时间复杂度: O(1)

辅助空间: O(1)