# Count numbers up to N whose rightmost set bit is K

Given two positive integers N and K, the task is to count the numbers from the range [1, N] whose Kth bit from the right, i.e. LSB, is the rightmost set bit.

Examples:

Input: N = 15, K = 2
Output: 4
Explanation:
(2)10 = (010)2, (6)10 = (110)2, (10)10 = (1010)2, (14)10 = (1110)2 have the 2nd bit from the right is set.

Input: N = 10 K = 3
Output: 3

Naive Approach: The idea is to iterate over the range [1, N] and for every number in the range, check if the position of the rightmost set bit is K and print the count of such numbers.
Time Complexity: O(N LogN)
Auxiliary Space: O(1)

Efficient Approach: The idea is to find the numbers with the position of the rightmost set bit at position i at every step. Follow the steps below to solve the problem:

• Iterate over the range [1, K] using a variable, say i.
• Find a number whose rightmost set bit is ith.
• Subtract that number from N.
• Repeat the above step for all values of i.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach`   `#include ` `using` `namespace` `std;`   `// Function to count the numbers in the` `// range [1, N] whose rightmost set bit is K` `int` `countNumberHavingKthBitSet(``int` `N, ``int` `K)` `{` `    ``// Stores the number whose` `    ``// rightmost set bit is K` `    ``int` `numbers_rightmost_setbit_K;`   `    ``for` `(``int` `i = 1; i <= K; i++) {`   `        ``// Numbers whose rightmost set bit is i` `        ``int` `numbers_rightmost_bit_i = (N + 1) / 2;`   `        ``// Subtracting the number whose` `        ``// rightmost set bit is i, from N` `        ``N -= numbers_rightmost_bit_i;`   `        ``// Since i = k, then the number whose` `        ``// rightmost set bit is K is stored` `        ``if` `(i == K) {` `            ``numbers_rightmost_setbit_K` `                ``= numbers_rightmost_bit_i;` `        ``}` `    ``}`   `    ``cout << numbers_rightmost_setbit_K;` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `N = 15;` `    ``int` `K = 2;` `    ``countNumberHavingKthBitSet(N, K);`   `    ``return` `0;` `}`

## Java

 `// Java program for the above approach` `import` `java.util.Arrays;` `class` `GFG` `{` `     `  `// Function to count the numbers in the` `// range [1, N] whose rightmost set bit is K` `static` `void` `countNumberHavingKthBitSet(``int` `N, ``int` `K)` `{` `    ``// Stores the number whose` `    ``// rightmost set bit is K` `    ``int` `numbers_rightmost_setbit_K = ``0``; ` `    ``for` `(``int` `i = ``1``; i <= K; i++) ` `    ``{` ` `  `        ``// Numbers whose rightmost set bit is i` `        ``int` `numbers_rightmost_bit_i = (N + ``1``) / ``2``;` ` `  `        ``// Subtracting the number whose` `        ``// rightmost set bit is i, from N` `        ``N -= numbers_rightmost_bit_i;` ` `  `        ``// Since i = k, then the number whose` `        ``// rightmost set bit is K is stored` `        ``if` `(i == K)` `        ``{` `            ``numbers_rightmost_setbit_K` `                ``= numbers_rightmost_bit_i;` `        ``}` `    ``} ` `    ``System.out.println(numbers_rightmost_setbit_K);` `}` ` `  `// Driver Code` `static` `public` `void` `main(String args[])` `{` `    ``int` `N = ``15``;` `    ``int` `K = ``2``;` `    ``countNumberHavingKthBitSet(N, K);` `}` `}`   `// This code is contributed by sanjoy_62`

## Python3

 `# Python3 program for the above approach`   `# Function to count the numbers in the` `# range [1, N] whose rightmost set bit is K` `def` `countNumberHavingKthBitSet(N, K):` `    `  `    ``# Stores the number whose` `    ``# rightmost set bit is K` `    ``numbers_rightmost_setbit_K ``=` `0`   `    ``for` `i ``in` `range``(``1``, K ``+` `1``):`   `        ``# Numbers whose rightmost set bit is i` `        ``numbers_rightmost_bit_i ``=` `(N ``+` `1``) ``/``/` `2`   `        ``# Subtracting the number whose` `        ``# rightmost set bit is i, from N` `        ``N ``-``=` `numbers_rightmost_bit_i`   `        ``# Since i = k, then the number whose` `        ``# rightmost set bit is K is stored` `        ``if` `(i ``=``=` `K):` `            ``numbers_rightmost_setbit_K ``=` `numbers_rightmost_bit_i`   `    ``print` `(numbers_rightmost_setbit_K)`   `# Driver Code` `if` `__name__ ``=``=` `'__main__'``:` `    ``N ``=` `15` `    ``K ``=` `2` `    ``countNumberHavingKthBitSet(N, K)`   `# This code is contributed by mohit kumar 29`

## C#

 `// C# program for the above approach` `using` `System;`   `class` `GFG` `{`   `  ``// Function to count the numbers in the` `  ``// range [1, N] whose rightmost set bit is K` `  ``static` `void` `countNumberHavingKthBitSet(``int` `N, ``int` `K)` `  ``{` `    ``// Stores the number whose` `    ``// rightmost set bit is K` `    ``int` `numbers_rightmost_setbit_K = 0; ` `    ``for` `(``int` `i = 1; i <= K; i++) ` `    ``{`   `      ``// Numbers whose rightmost set bit is i` `      ``int` `numbers_rightmost_bit_i = (N + 1) / 2;`   `      ``// Subtracting the number whose` `      ``// rightmost set bit is i, from N` `      ``N -= numbers_rightmost_bit_i;`   `      ``// Since i = k, then the number whose` `      ``// rightmost set bit is K is stored` `      ``if` `(i == K)` `      ``{` `        ``numbers_rightmost_setbit_K` `          ``= numbers_rightmost_bit_i;` `      ``}` `    ``} ` `    ``Console.WriteLine(numbers_rightmost_setbit_K);` `  ``}`   `  ``// Driver Code` `  ``static` `public` `void` `Main(String []args)` `  ``{` `    ``int` `N = 15;` `    ``int` `K = 2;` `    ``countNumberHavingKthBitSet(N, K);` `  ``}` `}`     `// This code is contributed by 29AjayKumar`

## Javascript

 ``

Output:

`4`

Time Complexity: O(K)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!