# M-th smallest number having k number of set bits.

Given two non-negative integers **m** and **k**. The problem is to find the **m-th** smallest number having **k** number of set bits.

**Constraints:** 1 <= m, k.

**Examples:**

Input : m = 4, k = 2 Output : 9 (9)_{10}= (1001)_{2}, it is the4thsmallest number having2set bits. Input : m = 6, k = 4 Output : 39

**Approach:** Following are the steps:

- Find the smallest number having
**k**number of set bits. Let it be**num**, where**num**= (1 << k) – 1. - Loop for
**m-1**times and each time replace**num**with the next higher number than ‘num’ having same number of bits as in ‘num’. Refer this post to find the required next higher number. - Finally return
**num**.

## C++

`// C++ implementation to find the mth smallest ` `// number having k number of set bits ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `typedef` `unsigned ` `int` `uint_t; ` ` ` `// function to find the next higher number ` `// with same number of set bits as in 'x' ` `uint_t nxtHighWithNumOfSetBits(uint_t x) ` `{ ` ` ` `uint_t rightOne; ` ` ` `uint_t nextHigherOneBit; ` ` ` `uint_t rightOnesPattern; ` ` ` ` ` `uint_t next = 0; ` ` ` ` ` `/* the approach is same as dicussed in ` ` ` `*/` ` ` `if` `(x) { ` ` ` `rightOne = x & -(` `signed` `)x; ` ` ` ` ` `nextHigherOneBit = x + rightOne; ` ` ` ` ` `rightOnesPattern = x ^ nextHigherOneBit; ` ` ` ` ` `rightOnesPattern = (rightOnesPattern) / rightOne; ` ` ` ` ` `rightOnesPattern >>= 2; ` ` ` ` ` `next = nextHigherOneBit | rightOnesPattern; ` ` ` `} ` ` ` ` ` `return` `next; ` `} ` ` ` `// function to find the mth smallest number ` `// having k number of set bits ` `int` `mthSmallestWithKSetBits(uint_t m, uint_t k) ` `{ ` ` ` `// smallest number having 'k' ` ` ` `// number of set bits ` ` ` `uint_t num = (1 << k) - 1; ` ` ` ` ` `// finding the mth smallest number ` ` ` `// having k set bits ` ` ` `for` `(` `int` `i = 1; i < m; i++) ` ` ` `num = nxtHighWithNumOfSetBits(num); ` ` ` ` ` `// required number ` ` ` `return` `num; ` `} ` ` ` `// Driver program to test above ` `int` `main() ` `{ ` ` ` `uint_t m = 6, k = 4; ` ` ` `cout << mthSmallestWithKSetBits(m, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find the mth ` `# smallest number having k number of set bits ` ` ` `# function to find the next higher number ` `# with same number of set bits as in 'x' ` `def` `nxtHighWithNumOfSetBits(x): ` ` ` `rightOne ` `=` `0` ` ` `nextHigherOneBit ` `=` `0` ` ` `rightOnesPattern ` `=` `0` ` ` ` ` `next` `=` `0` ` ` ` ` `""" the approach is same as dicussed in ` ` ` `http:#www.geeksforgeeks.org/next-higher-number-with-same-number-of-set-bits/ ` ` ` `"""` ` ` `if` `(x): ` ` ` `rightOne ` `=` `x & (` `-` `x) ` ` ` `nextHigherOneBit ` `=` `x ` `+` `rightOne ` ` ` ` ` `rightOnesPattern ` `=` `x ^ nextHigherOneBit ` ` ` ` ` `rightOnesPattern ` `=` `(rightOnesPattern) ` `/` `/` `rightOne ` ` ` ` ` `rightOnesPattern >>` `=` `2` ` ` ` ` `next` `=` `nextHigherOneBit | rightOnesPattern ` ` ` ` ` `return` `next` ` ` `# function to find the mth smallest ` `# number having k number of set bits ` `def` `mthSmallestWithKSetBits(m, k): ` ` ` ` ` `# smallest number having 'k' ` ` ` `# number of set bits ` ` ` `num ` `=` `(` `1` `<< k) ` `-` `1` ` ` ` ` `# finding the mth smallest number ` ` ` `# having k set bits ` ` ` `for` `i ` `in` `range` `(` `1` `, m): ` ` ` `num ` `=` `nxtHighWithNumOfSetBits(num) ` ` ` ` ` `# required number ` ` ` `return` `num ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `m ` `=` `6` ` ` `k ` `=` `4` ` ` `print` `(mthSmallestWithKSetBits(m, k)) ` ` ` `# This code is contributed by ` `# Shubham Singh(SHUBHAMSINGH10) ` |

*chevron_right*

*filter_none*

**Output:**

39

**Time Complexity:** O(m)

This article is contributed by **Ayush Jauhari**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

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.

## Recommended Posts:

- Smallest number whose set bits are maximum in a given range
- Find the smallest number with n set and m unset bits
- Check if bits of a number has count of consecutive set bits in increasing order
- Find the largest number smaller than integer N with maximum number of set bits
- Maximize a given unsigned number number by swapping bits at it's extreme positions.
- Check if a number has same number of set and unset bits
- Toggle bits of a number except first and last bits
- Minimum number using set bits of a given number
- Next higher number with same number of set bits
- Number of integers with odd number of set bits
- Set all even bits of a number
- Set all odd bits of a number
- Same Number Of Set Bits As N
- Toggle all the bits of a number except k-th bit.
- Swap bits in a given number
- Check whether the number has only first and last bits set
- Number with set bits only between L-th and R-th index
- Check if all bits of a number are set
- Toggle all even bits of a number
- Toggle all odd bits of a number