Kth smallest positive integer Y such that its sum with X is same as its bitwise OR with X
Last Updated :
14 Sep, 2021
Given two positive integers X and K, the task is to find the Kth smallest positive integer (Y) such that the sum of X and Y is equal to Bitwise OR of X and Y, i.e. (X + Y = X | Y)
Examples:
Input: X = 5, K = 1
Output: 2
Explanation:
Consider the smallest number as 2. The sum of 2 and 5 is 2 + 5 = 7 and the Bitwise OR of 2 and 5 is 7.
Input: X = 5, K = 5
Output: 18
Approach: The given problem can be solved by below observation:
(X + Y) = (X & Y) + (X | Y)
- Therefore, for the value of X + Y to be same as X | Y, the value of X & Y must be 0.
- Also, we know that for X & Y to be 0, the position of set bits in X must be unset in Y, and the positions of unset bits of X can be either 0 or 1 in Y.
- Hence, to find Kth smallest positive number meeting above conditions, we can use combinations for the bit positions of Y that are unset in X.
To implement the above, iterate the binary representation of X from right to left, and at every iteration, consider the following cases:
- If the current bit of X is 1, the corresponding bit in Y will be 0, to get (X & Y) as 0.
- If the current bit of X is 0 and rightmost bit of K is 1, the corresponding bit of Y will be 1. This means that two combinations at current bit in Y has been used - first 0 and then 1. Then divide K by 2
- If the current bit of X is 0 and rightmost bit of K is 0, the corresponding bit of Y will be 0. This means that only one combination at current bit in Y has been used - 0. Then divide K by 2
- If K becomes 0, break the loop, stating that the Kth number has been found.
Finally, print the decimal conversion of Y as the required answer.
Below is the implementation of the above approach:
C++
// C++ implementation for the above approach
#include <bits/stdc++.h>
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
<script>
// JavaScript Program to implement
// the above approach
// Function to calculate K-th smallest
// solution(Y) of equation X+Y = X|Y
function KthSolution(X,K)
{
// Initialize the variable
// to store the answer
let ans = 0;
for (let i = 0; i < 64; i++) {
// The i-th bit of X is off
if (!(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 (!K) {
break;
}
}
}
return ans;
}
// Driver Code
let X = 10, K = 5;
document.write(KthSolution(X, K));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(log(X))
Auxiliary Space: O(1)
Similar Reads
Smallest element with K set bits such that sum of Bitwise AND of each array element with K is maximum Given an array arr[] consisting of N integers and integer K, the task is to find the smallest integer X with exactly K set bits such that the sum of Bitwise AND of X with every array element arr[i] is maximum. Examples: Input: arr[] = {3, 4, 5, 1}, K = 1Output: 4Explanation: Consider the value of X
8 min read
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 = 1Output: 2Explanation: The first number is 2 as (2 + 5 = 2 | 5 ) Input: X = 5, K = 5Output: 18Explanation: The
5 min read
Find smallest positive number Y such that Bitwise AND of X and Y is Zero Given an integer X. The task is to find the smallest positive number Y(> 0) such that X AND Y is zero.Examples: Input : X = 3 Output : 4 4 is the smallest positive number whose bitwise AND with 3 is zero Input : X = 10 Output : 1 Approach : There are 2 cases : If the binary representation of X co
6 min read
Minimum integer with at most K bits set such that their bitwise AND with N is maximum Given an integer N which can be represented in 32 bit, the task is to find another integer X that has at most K bits set in its binary representation and bitwise AND of X and N is maximum. Examples: Input: N = 5, K = 1 Output: X = 2 Explanation: Binary representation of 5 is 101, the possible value
7 min read
Min sum of set of positive integers so that sum of no two elements is K Given two positive integers N and K. Create a set of size N containing distinct positive integers such that the sum of no two integers is K, the task is to find the minimum possible sum of the set that meets the above criteria. Since the answer may be large, print it modulo 10^9+7. Note: It is guara
6 min read
Smallest number greater or equals to N such that it has no odd positioned bit set Given an integer N, the task is to find the smallest integer X such that it has no odd position set and X ? N. Note: The positioning of bits is assumed from the right side and the first bit is assumed to be the 0th bit. Examples: Input: N = 9 Output: 16 16's binary representation is 10000, which has
12 min read