The Wayback Machine - https://web.archive.org/web/20210826092744/https://www.geeksforgeeks.org/kth-smallest-positive-integer-y-such-that-its-sum-with-x-is-same-as-its-bitwise-or-with-x/
Skip to content
Related Articles

Related Articles

Improve Article

Kth smallest positive integer Y such that its sum with X is same as its bitwise OR with X

  • Last Updated : 25 Aug, 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:



  • Using the properties of bitwise operator on two numbers X and Y:

    (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 righmost 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 righmost 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;
}
Output:
17

Time Complexity: O(log(X))
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up