# Largest number less than X having at most K set bits

Given an integer X > 1 and an integer K > 0, the task is to find the greatest odd number < X such that the number of 1’s in its binary representation is at most K.

Examples:

Input: X = 10, K = 2
Output: 10

Input: X = 29, K = 2
Output: 24

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach: Starting from X – 1 check all the numbers below X which have at most K set bits, the first number satisfying the condition is the required answer.

Efficient Approach: is to count the set bits. If the count is less than or equal to K, return X. Otherwise, keep removing righmost set bit while count – k does not become 0.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the greatest number <= X ` `// having at most K set bits. ` `int` `greatestKBits(``int` `X, ``int` `K) ` `{ ` `    ``int` `set_bit_count = __builtin_popcount(X); ` `    ``if` `(set_bit_count <= K) ` `        ``return` `X; ` ` `  `    ``// Remove rightmost set bits one ` `    ``// by one until we count becomes k ` `    ``int` `diff = set_bit_count - K; ` `    ``for` `(``int` `i = 0; i < diff; i++) ` `        ``X &= (X - 1); ` ` `  `    ``// Return the required number ` `    ``return` `X; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `X = 21, K = 2; ` `    ``cout << greatestKBits(X, K); ` `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.io.*;  ` `   `  `class` `GFG {  ` ` `  `    ``// Function to return the greatest number <= X ` `    ``// having at most K set bits. ` `     ``int` `greatestKBits(``int` `X, ``int` `K) ` `     ``{ ` `       ``int` `set_bit_count = Integer.bitCount(X); ` `       ``if` `(set_bit_count <= K) ` `            ``return` `X; ` ` `  `        ``// Remove rightmost set bits one ` `        ``// by one until we count becomes k ` `        ``int` `diff = set_bit_count - K; ` `        ``for` `(``int` `i = ``0``; i < diff; i++) ` `            ``X &= (X - ``1``); ` ` `  `        ``// Return the required number ` `        ``return` `X; ` `    ``} ` ` `  `// Driver code ` `public` `static` `void` `main (String[] args)  ` `{  ` `    ``int` `X = ``21``, K = ``2``; ` `    ``GFG g=``new` `GFG(); ` `      ``System.out.print(g.greatestKBits(X, K)); ` `} ` ` `  `//This code is contributed by Shivi_Aggarwal ` `} `

## Python3

 `# Python 3 implementation of the approach ` ` `  `# Function to return the greatest  ` `# number <= X having at most K set bits. ` `def` `greatestKBits(X, K): ` `    ``set_bit_count ``=` `bin``(X).count(``'1'``) ` `    ``if` `(set_bit_count <``=` `K): ` `        ``return` `X ` ` `  `    ``# Remove rightmost set bits one ` `    ``# by one until we count becomes k ` `    ``diff ``=` `set_bit_count ``-` `K ` `    ``for` `i ``in` `range``(``0``, diff, ``1``): ` `        ``X &``=` `(X ``-` `1``) ` ` `  `    ``# Return the required number ` `    ``return` `X ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``X ``=` `21` `    ``K ``=` `2` `    ``print``(greatestKBits(X, K)) ` `     `  `# This code is contributed by ` `# Shashank_Sharma `

## C#

 `// C# implementation of the above approach  ` `using` `System; ` ` `  `class` `GFG ` `{  ` `    ``// Function to get no of set  ` `    ``// bits in binary representation  ` `    ``// of positive integer n  ` `    ``static` `int` `countSetBits(``int` `n)  ` `    ``{  ` `        ``int` `count = 0;  ` `        ``while` `(n > 0)  ` `        ``{  ` `            ``count += n & 1;  ` `            ``n >>= 1;  ` `        ``}  ` `        ``return` `count;  ` `    ``}  ` `     `  `    ``// Function to return the greatest number <= X  ` `    ``// having at most K set bits.  ` `    ``static` `int` `greatestKBits(``int` `X, ``int` `K)  ` `    ``{  ` `        ``int` `set_bit_count = countSetBits(X);  ` `        ``if` `(set_bit_count <= K)  ` `        ``return` `X;  ` ` `  `        ``// Remove rightmost set bits one  ` `        ``// by one until we count becomes k  ` `        ``int` `diff = set_bit_count - K;  ` `        ``for` `(``int` `i = 0; i < diff; i++)  ` `            ``X &= (X - 1);  ` ` `  `        ``// Return the required number  ` `        ``return` `X;  ` `    ``}  ` ` `  `    ``// Driver code  ` `    ``public` `static` `void` `Main()  ` `    ``{  ` `        ``int` `X = 21, K = 2;  ` `        ``Console.WriteLine(greatestKBits(X, K));  ` `         `  `    ``}  ` `}  ` ` `  `// This code is contributed by Ryuga `

Output:

```20
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.