Given an integer X. The task is to find the smallest positive number Y(> 0) such that X AND Y is zero.
Input : X = 3
Output : 4
4 is the samllest positive number whose bitwise AND with 3 is zero
Input : X = 10
Output : 1
There are 2 cases :
- If the binary representation of X contains all 1s, in that case, all the bits of Y should be 0 to make the result of AND operation is zero. Then X+1 is our answer which is the first positive integer.
- If the binary representation of X doesn’t contain all 1s, in that case, find the first position in X at which bit is 0. Then our answer will be power(2, position)
Below is the implementation of the above approach :
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find the smallest positive number which can not be represented by given digits
- Find the smallest positive number missing from an unsorted array | Set 3
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Find a positive number M such that gcd(N^M, N&M) is maximum
- Find Nth positive number whose digital root is X
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Find number of edges that can be broken in a tree such that Bitwise OR of resulting two trees are equal
- Find smallest possible Number from a given large Number with same count of digits
- Find the smallest number whose digits multiply to a given number n
- Size of the smallest subset with maximum Bitwise OR
- Find K'th smallest number such that A + B = A | B
- Find smallest number K such that K % p = 0 and q % K = 0
- Find smallest number n such that n XOR n+1 equals to given k.
- Find the Smallest number that divides X^X
- Given a number, find the next smallest palindrome
- Find the kth smallest number with sum of digits as m
- Find the smallest number whose sum of digits is N
- Find smallest permutation of given number
- Find Nth smallest number that is divisible by 100 exactly K times
- Find the smallest number X such that X! contains at least Y trailing zeros.
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.