第 K 个最小的正整数,其和与给定数等于按位或与给定数
给定两个正整数X和K ,任务是找到第K个最小的正整数Y,使得X + Y = X | Y , 其中 |表示按位或运算。
例子:
Input: X = 5, K = 1
Output: 2
Explanation: The first number is 2 as (2 + 5 = 2 | 5 )
Input: X = 5, K = 5
Output: 18
Explanation: The list of correct values is 2, 8, 10, 16, 18. The 5th number is this list is 18
方法:给定问题可以解决 按照下面提到的步骤:
- 让最终值为Y 。从按位运算的性质可知, X + Y = X & Y + X | Y ,其中 & 是两个数字的按位与
- 要使问题陈述中的等式为真, X & Y的值必须为 0
- 因此,对于所有位置,如果第 i 位在X中为 ON,那么对于 Y 的所有可能解,它必须为 OFF
- 例如,如果X = 1001101001 二进制(617 十进制),那么 y 的最后十位必须是Y = 0**00*0**0,其中 '*' 表示零或一。此外,我们可以将任意数量的任意数字填充到数字的开头,因为所有高位都是零
- 所以最终的解决方案是将所有位可以为 0 或 1 的位置视为从左到右的序列,并找到K的二进制表示法。
- 根据K的二进制符号填充所有位置
下面是上述方法的实现:
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 = 5, K = 5;
cout << KthSolution(X, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
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 = 5, K = 5;
System.out.println(KthSolution(X, K));
}
}
// This code is contributed by sanjoy_62.
Python3
# python implementation for 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(0, 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
if __name__ == "__main__":
X = 5
K = 5
print(KthSolution(X, K))
# This code is contributed by rakeshsahni
C#
// C# implementation for the above approach
using System;
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 & (1LL << i)) == 0) {
// The i-bit of K is on
if ((K & 1) > 0) {
ans |= (1LL << 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()
{
long X = 5, K = 5;
Console.Write(KthSolution(X, K));
}
}
// This code is contributed by ukasp.
Javascript
输出
18
时间复杂度: O(log(N))
辅助空间: O(1)