📌  相关文章
📜  找出一个数字X,使(X XOR A)最小,并且X和B中的置位数相等

📅  最后修改于: 2021-05-07 18:53:46             🧑  作者: Mango

给定两个整数AB ,任务是找到一个整数X ,使得(X XOR A)最小可能,并且X中的设置位计数等于B中的设置位计数。

例子:

方法:已知元素与自身的异或为0。因此,请尝试生成M的二进制表示形式,该表示形式应尽可能接近A。从A中的最高有效位遍历到最低有效位,如果在当前位置设置了一位,则还需要将其设置为所需的数量,以使XOR最小化,但是设置的位数必须相等因此,当所需数量中的设置位数达到B中的设置位数时,其余位数必须为0。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the value x
// such that (x XOR a) is minimum
// and the number of set bits in x
// is equal to the number
// of set bits in b
int minVal(int a, int b)
{
    // Count of set-bits in bit
    int setBits = __builtin_popcount(b);
    int ans = 0;
  
    for (int i = 30; i >= 0; i--) {
        int mask = 1 << i;
        bool set = a & mask;
  
        // If i'th bit is set also set the
        // same bit in the required number
        if (set && setBits > 0) {
            ans |= (1 << i);
  
            // Decrease the count of setbits
            // in b as the count of set bits
            // in the required number has to be
            // equal to the count of set bits in b
            setBits--;
        }
    }
  
    return ans;
}
  
// Driver code
int main()
{
    int a = 3, b = 5;
  
    cout << minVal(a, b);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{ 
    // Function to get no of set 
    // bits in binary representation 
    // of positive integer n 
    static int countSetBits(int n) 
    { 
        int count = 0; 
        while (n > 0) 
        { 
            count += n & 1; 
            n >>= 1; 
        } 
        return count; 
    } 
  
// Function to return the value x
// such that (x XOR a) is minimum
// and the number of set bits in x
// is equal to the number
// of set bits in b
static int minVal(int a, int b)
{
    // Count of set-bits in bit
    int setBits = countSetBits(b);
    int ans = 0;
  
    for (int i = 30; i >= 0; i--) 
    {
        int mask = 1 << i;
          
        // If i'th bit is set also set the
        // same bit in the required number
        if ((a & mask) > 0 && setBits > 0) 
        {
            ans |= (1 << i);
              
            // Decrease the count of setbits
            // in b as the count of set bits
            // in the required number has to be
            // equal to the count of set bits in b
            setBits--;
        }
    }
    return ans;
}
  
// Driver Code 
public static void main(String[] args) 
{ 
    int a = 3, b = 5;
  
    System.out.println(minVal(a, b));
} 
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach 
  
# Function to return the value x 
# such that (x XOR a) is minimum 
# and the number of set bits in x 
# is equal to the number 
# of set bits in b 
def minVal(a, b) : 
  
    # Count of set-bits in bit 
    setBits = bin(b).count('1'); 
    ans = 0; 
  
    for i in range(30, -1, -1) :
        mask = (1 << i); 
        s = (a & mask); 
  
        # If i'th bit is set also set the 
        # same bit in the required number 
        if (s and setBits > 0) :
            ans |= (1 << i); 
  
            # Decrease the count of setbits 
            # in b as the count of set bits 
            # in the required number has to be 
            # equal to the count of set bits in b 
            setBits -= 1; 
  
    return ans; 
  
# Driver code 
if __name__ == "__main__" : 
  
    a = 3; b = 5; 
  
    print(minVal(a, b)); 
  
# This code is contributed by kanugargng


C#
// C# implementation of the approach
using System; 
  
class GFG
{ 
    // Function to get no of set 
    // bits in binary representation 
    // of positive integer n 
    static int countSetBits(int n) 
    { 
        int count = 0; 
        while (n > 0) 
        { 
            count += n & 1; 
            n >>= 1; 
        } 
        return count; 
    } 
  
// Function to return the value x
// such that (x XOR a) is minimum
// and the number of set bits in x
// is equal to the number
// of set bits in b
static int minVal(int a, int b)
{
    // Count of set-bits in bit
    int setBits = countSetBits(b);
    int ans = 0;
  
    for (int i = 30; i >= 0; i--) 
    {
        int mask = 1 << i;
          
        // If i'th bit is set also set the
        // same bit in the required number
        if ((a & mask) > 0 && setBits > 0) 
        {
              
            ans |= (1 << i);
              
            // Decrease the count of setbits
            // in b as the count of set bits
            // in the required number has to be
            // equal to the count of set bits in b
            setBits--;
        }
    }
  
    return ans;
}
  
// Driver Code 
public static void Main() 
{ 
    int a = 3, b = 5;
  
    Console.Write(minVal(a, b));
} 
} 
  
// This code is contributed by Mohit kumar 29


输出:
3

时间复杂度: O(log(N))