📌  相关文章
📜  第 K 个最小的正整数 Y 使得它与 X 的和等于它与 X 的按位或

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

第 K 个最小的正整数 Y 使得它与 X 的和等于它与 X 的按位或

给定两个正整数XK ,任务是找到第K最小的正整数 ( Y ),使得XY的和等于XY的按位或,即(X + Y = X | Y)

例子:

方法:给定的问题可以通过以下观察来解决:

  • 在两个数字XY上使用位运算运算符的属性:
  • 因此,对于X + Y的值与X | YX & Y的值必须为0
  • 另外,我们知道要使X & Y为 0,X 中的设置位在 Y 中的位置必须是未设置的,而 X 中未设置的位在 Y 中的位置可以是 0 或 1。
  • 因此,要找到满足上述条件的第 K 个最小的正数,我们可以使用 X 中未设置的 Y 的位位置的组合。

为了实现上述,从右到左迭代X的二进制表示,并且在每次迭代时,考虑以下情况:

  • 如果X的当前位为 1,则 Y 中的对应位为 0,则 (X & Y) 为 0。
  • 如果X的当前位为 0,而 K 的最右边位为 1,则 Y 的对应位将为 1。这意味着 Y 中当前位的两个组合已被使用——首先是 0,然后是 1。然后将K除以 2
  • 如果X的当前位为 0,而 K 的最右边位为 0,则 Y 的对应位将为 0。这意味着 Y 中的当前位仅使用了一种组合 – 0。然后将K除以 2
  • 如果K变为 0,则中断循环,说明已找到第 K 个数。

最后,打印 Y 的十进制转换作为所需答案。

下面是上述方法的实现:

C++
// C++ implementation for the above approach
#include 
using namespace std;
 
// Function to calculate K-th smallest
// solution(Y) of equation X+Y = X|Y
long long KthSolution(long long X,
                      long long K)
{
    // Initialize the variable
      // to store the answer
    long long ans = 0;
 
    for (int i = 0; i < 64; i++) {
       
        // The i-th bit of X is off
        if (!(X & (1LL << i))) {
           
            // The i-bit of K is on
            if (K & 1) {
                ans |= (1LL << i);
            }
           
            // Divide K by 2
            K >>= 1;
 
            // If K becomes 0 then break
            if (!K) {
                break;
            }
        }
    }
    return ans;
}
// Driver Code
int main()
{
    long long X = 10, K = 5;
    cout << KthSolution(X, K);
    return 0;
}


Java
/*package whatever //do not write package name here */
import java.io.*;
 
class GFG
{
   
    // Function to calculate K-th smallest
    // solution(Y) of equation X+Y = X|Y
   static long KthSolution(long X, long K)
    {
      
        // Initialize the variable
        // to store the answer
        long ans = 0;
 
        for (int i = 0; i < 64; i++) {
 
            // The i-th bit of X is off
            if ((X & (1 << i)) == 0) {
 
                // The i-bit of K is on
                if ((K & 1) > 0) {
                    ans |= (1 << i);
                }
 
                // Divide K by 2
                K >>= 1;
 
                // If K becomes 0 then break
                if (K == 0) {
                    break;
                }
            }
        }
        return ans;
    }
   
    // Driver Code
    public static void main(String[] args)
    {
        long X = 10;
        long K = 5;
        System.out.println(KthSolution(X, K));
    }
}
 
// This code is contributed by maddler.


Python3
# Python Program to implement
# the above approach
 
# Function to calculate K-th smallest
# solution(Y) of equation X+Y = X|Y
def KthSolution(X, K):
   
    # Initialize the variable
    # to store the answer
    ans = 0
 
    for i in range(64):
 
        # The i-th bit of X is off
        if not (X & (1 << i)):
 
            # The i-bit of K is on
            if (K & 1):
                ans |= (1 << i)
             
 
            # Divide K by 2
            K >>= 1
 
            # If K becomes 0 then break
            if not K:
                break
             
    return ans
 
# Driver Code
X = 10
K = 5
print(KthSolution(X, K))
 
# This code is contributed by Saurabh Jaiswal


C#
/*package whatever //do not write package name here */
using System;
 
public class GFG
{
   
    // Function to calculate K-th smallest
    // solution(Y) of equation X+Y = X|Y
   static long KthSolution(long X, long K)
    {
      
        // Initialize the variable
        // to store the answer
        int ans = 0;
 
        for (int i = 0; i < 64; i++) {
 
            // The i-th bit of X is off
            if ((X & (1 << i)) == 0) {
 
                // The i-bit of K is on
                if ((K & 1) > 0) {
                    ans |= (1 << i);
                }
 
                // Divide K by 2
                K >>= 1;
 
                // If K becomes 0 then break
                if (K == 0) {
                    break;
                }
            }
        }
        return ans;
    }
   
    // Driver Code
    public static void Main(String[] args)
    {
        long X = 10;
        long K = 5;
        Console.WriteLine(KthSolution(X, K));
    }
}
 
// This code is contributed by Princi Singh


Javascript


输出:
17

时间复杂度: O(log(X))
辅助空间: O(1)