Skip to content
Related Articles

Related Articles

Find Kth smallest value for b such that a + b = a | b
  • Difficulty Level : Easy
  • Last Updated : 28 Jan, 2020

Given a number a and k, the task is to find the k’th smallest value for b such that a + b = a | b, where ‘|’ denotes the bitwise OR operator. The maximum value of a and k can be 10^9.

Examples:

Input: a = 10, k = 3
Output: 5
Numbers satisfying the condition 5 + b = 5 | b
are 1, 4, 5, etc.
Since 3rd smallest value for b is required,
hence b = 5.

Input: a = 1, k = 1
Output: 2
Numbers satisfying the condition 1 + b = 1 | b
are 2, 4, 6, etc.
Since 1st smallest value for b is required,
hence b = 2.

Approach:

  • b is a solution of the given equation if and only if b has 0 in all positions where a has 1 (in binary notation).
  • So, we need to determine the bit of b for positions where a has 0. Let, if a = 10100001 then the last eight digits of b must be b = 0*0****0, where ‘*’ denotes either 0 or 1. Any replacement of all ‘*’ by 0 or 1 gives us a solution.
  • The k-th smallest number will be received by replacing all ‘*’ in y by digits of binary representation of number k.
  • As maximum value of a and k is 10^9, so checking upto 32 positions in binary representation is enough to get the correct answer for the given equation.

Below is the implementation of above Approach:

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find k'th smallest value for b
// such that a + b = a | b
  
#include <bits/stdc++.h>
using namespace std;
  
#define ll long long
  
// Function to find
// the kth smallest value for b
ll kthSmallest(ll a, ll k)
{
  
    // res will store final answer
    ll res = 0;
    ll j = 0;
    for (ll i = 0; i < 32; i++) {
  
        // skip when j'th position
        // has 1 in binary representation
        // as in res, j'th position will be 0.
  
        while (j < 32 && (a & (1 << j)))
            // j'th bit is set
            j++;
  
        // if i'th bit of k is 1
        // and i'th bit of j is 0
        // then set i'th bit in res.
  
        if (k & (1 << i)) // i'th bit is set
            res |= (1LL << j);
  
        // proceed to next bit
        j++;
    }
    return res;
}
  
// Driver Code
int main()
{
  
    ll a = 10, k = 3;
  
    cout << kthSmallest(a, k) << "\n";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find k'th smallest value for b
// such that a + b = a | b
class GFG
{
  
    // Function to find
    // the kth smallest value for b
    static int kthSmallest(int a, int k) 
    {
  
        // res wiint store final answer
        int res = 0;
        int j = 0;
        for (int i = 0; i < 32; i++)
        {
  
            // skip when j'th position
            // has 1 in binary representation
            // as in res, j'th position wiint be 0.
            while (j < 32 && (a & (1 << j)) > 0)
                  
                // j'th bit is set
                j++;
  
            // if i'th bit of k is 1
            // and i'th bit of j is 0
            // then set i'th bit in res.
            if ((k & (1 << i)) > 0) // i'th bit is set
                res |= (1 << j);
  
            // proceed to next bit
            j++;
        }
        return res;
    }
  
    // Driver Code
    public static void main(String[] args) 
    {
        int a = 10, k = 3;
  
        System.out.print(kthSmallest(a, k) + "\n");
    }
}
  
// This code is contributed by Rajput-Ji

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find k'th smallest value for b
# such that a + b = a | b
  
# Function to find
# the kth smallest value for b
def kthSmallest(a, k):
  
    # res wistore final answer
    res = 0
    j = 0
    for i in range(32):
  
        # skip when j'th position
        # has 1 in binary representation
        # as in res, j'th position wibe 0.
        while (j < 32 and (a & (1 << j))):
              
            # j'th bit is set
            j += 1
  
        # if i'th bit of k is 1
        # and i'th bit of j is 0
        # then set i'th bit in res.
  
        if (k & (1 << i)): # i'th bit is set
            res |= (1 << j)
  
        # proceed to next bit
        j += 1
    return res
  
# Driver Code
  
a = 10
k = 3
  
print(kthSmallest(a, k))
  
# This code is contributed by mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find k'th smallest value for b
// such that a + b = a | b
using System;
  
class GFG
{
  
    // Function to find
    // the kth smallest value for b
    static int kthSmallest(int a, int k) 
    {
  
        // res wiint store final answer
        int res = 0;
        int j = 0;
        for (int i = 0; i < 32; i++)
        {
  
            // skip when j'th position
            // has 1 in binary representation
            // as in res, j'th position wiint be 0.
            while (j < 32 && (a & (1 << j)) > 0)
                  
                // j'th bit is set
                j++;
  
            // if i'th bit of k is 1
            // and i'th bit of j is 0
            // then set i'th bit in res.
            if ((k & (1 << i)) > 0) // i'th bit is set
                res |= (1 << j);
  
            // proceed to next bit
            j++;
        }
        return res;
    }
  
    // Driver Code
    public static void Main() 
    {
        int a = 10, k = 3;
  
        Console.WriteLine(kthSmallest(a, k));
    }
}
  
// This code is contributed by AnkitRai01

chevron_right


Output:

5

Time Complexity: O(N)

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :