Given two non-negative numbers n and m. The problem is to find the smallest number having n number of set bits and m number of unset bits in its binary representation. Constraints: 1 <= n, 0 <= m, (m+n) <= 31 Note : 0 bits before leading 1 (or leftmost 1) in binary representation are counted
Input : n = 2, m = 2
Output : 9
(9)10 = (1001)2
We can see that in the binary representation of 9
there are 2 set and 2 unsets bits and it is the
Input : n = 4, m = 1
Output : 23
Space Complexity : O(1) For greater values of n and m, you can use long int and long long int datatypes to generate the required number. This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...