Given two number A and K, the task is to find K’th smallest positive integer B such that A + B = A | B, where | denotes the bitwise OR operator.
Input: A = 10, K = 3 Output: 5 Explanation: K = 1, 10 + 1 = 10 | 1 = 11 K = 2, 10 + 4 = 10 | 4 = 14 K = 3, 10 + 5 = 10 | 5 = 15 Input: A = 1, B = 1 Output: 2
- 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 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.
Below is the implementation of the above approach:
Time Complexity: O(log(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.
- Find smallest perfect square number A such that N + A is also a perfect square number
- Find smallest number n such that n XOR n+1 equals to given k.
- Find smallest number K such that K % p = 0 and q % K = 0
- Find the smallest number X such that X! contains at least Y trailing zeros.
- Find smallest positive number Y such that Bitwise AND of X and Y is Zero
- Find smallest values of x and y such that ax - by = 0
- Find Kth smallest value for b such that a + b = a | b
- Smallest number S such that N is a factor of S factorial or S!
- Smallest number k such that the product of digits of k is equal to n
- Split the number into N parts such that difference between the smallest and the largest part is minimum
- Smallest number greater or equals to N such that it has no odd positioned bit set
- Find the smallest number whose digits multiply to a given number n
- Find smallest number with given number of digits and sum of digits under given constraints
- Find smallest possible Number from a given large Number with same count of digits
- Find smallest number formed by inverting digits of given number N
- Smallest index such that there are no 0 or 1 to its right
- Smallest x such that 1*n, 2*n, ... x*n have all digits from 1 to 9
- Smallest divisor D of N such that gcd(D, M) is greater than 1
- Partition N into M parts such that difference between Max and Min part is smallest
- Smallest integer greater than n such that it consists of digit m exactly k times
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : himanshu77