K-th smallest positive integer having sum with given number equal to bitwise OR with given number
Given two positive integers X and K, the task is to find the K-th smallest positive integer Y, such that X + Y = X | Y, where | denotes the bitwise OR operation.
Example:
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
Approach: Given problem can be solved following the below mentioned steps:
- Let the final value be Y. From the properties of bitwise operations, it is known that X + Y = X & Y + X | Y, where & is a bitwise AND of two numbers
- For the equation in the problem statement to be true, the value of X & Y must be 0
- So for all positions, if the ith bit is ON in X then it must be OFF for all possible solutions of Y
- For instance, if X = 1001101001 in binary (617 in decimal notation), then the last ten digits of y must be Y= 0**00*0**0, where ‘*’ denotes either zero or one. Also, we can pad any number of any digits to the beginning of the number, since all higher bits are zeroes
- So the final solution will be to treat all the positions where the bit can be 0 or 1 as a sequence from left to right and find the binary notation of K.
- Fill all positions according to the binary notation of K
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long KthSolution( long long X, long long K)
{
long long ans = 0;
for ( int i = 0; i < 64; i++) {
if (!(X & (1LL << i))) {
if (K & 1) {
ans |= (1LL << i);
}
K >>= 1;
if (!K) {
break ;
}
}
}
return ans;
}
int main()
{
long long X = 5, K = 5;
cout << KthSolution(X, K);
return 0;
}
|
Java
import java.util.*;
class GFG {
static long KthSolution( long X, long K)
{
long ans = 0 ;
for ( int i = 0 ; i < 64 ; i++) {
if ((X & ( 1 << i)) == 0 ) {
if ((K & 1 ) > 0 ) {
ans |= ( 1 << i);
}
K >>= 1 ;
if (K == 0 ) {
break ;
}
}
}
return ans;
}
public static void main(String[] args)
{
long X = 5 , K = 5 ;
System.out.println(KthSolution(X, K));
}
}
|
Python3
def KthSolution(X, K):
ans = 0
for i in range ( 0 , 64 ):
if ( not (X & ( 1 << i))):
if (K & 1 ):
ans | = ( 1 << i)
K >> = 1
if ( not K):
break
return ans
if __name__ = = "__main__" :
X = 5
K = 5
print (KthSolution(X, K))
|
C#
using System;
class GFG
{
static long KthSolution( long X, long K)
{
long ans = 0;
for ( int i = 0; i < 64; i++) {
if ((X & (1LL << i)) == 0) {
if ((K & 1) > 0) {
ans |= (1LL << i);
}
K >>= 1;
if (K == 0) {
break ;
}
}
}
return ans;
}
public static void Main()
{
long X = 5, K = 5;
Console.Write(KthSolution(X, K));
}
}
|
Javascript
<script>
function KthSolution(X, K)
{
let ans = 0;
for (let i = 0; i < 64; i++) {
if (!(X & (1 << i))) {
if (K & 1) {
ans |= (1 << i);
}
K >>= 1;
if (!K) {
break ;
}
}
}
return ans;
}
let X = 5, K = 5;
document.write(KthSolution(X, K));
</script>
|
Time Complexity: O(log(N))
Auxiliary Space: O(1)
Last Updated :
20 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...