# 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 K^{th} bit from the right, i.e. LSB, is the rightmost set bit.

**Examples:**

Input:N = 15, K = 2Output:4Explanation:

(2)_{10}= (010)_{2}, (6)_{10}= (110)_{2}, (10)_{10}= (1010)_{2}, (14)_{10}= (1110)_{2}have the 2^{nd bit from the right is set.}

Input:N = 10 K = 3Output: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
**i**^{th}. - 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 <bits/stdc++.h>` `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` ` ` `// rigthmost 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` ` ` `// rigthmost 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` ` ` `# rigthmost 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` ` ` `// rigthmost 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

`<script>` `// javascript program for the above approach` `// Function to count the numbers in the` `// range [1, N] whose rightmost set bit is K` `function` `countNumberHavingKthBitSet(N, K)` `{` ` ` `// Stores the number whose` ` ` `// rightmost set bit is K` ` ` `let numbers_rightmost_setbit_K = 0;` ` ` `for` `(let i = 1; i <= K; i++)` ` ` `{` ` ` ` ` `// Numbers whose rightmost set bit is i` ` ` `let 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` ` ` `// rigthmost set bit is K is stored` ` ` `if` `(i == K)` ` ` `{` ` ` `numbers_rightmost_setbit_K` ` ` `= numbers_rightmost_bit_i;` ` ` `}` ` ` `}` ` ` `document.write(numbers_rightmost_setbit_K);` `}` ` ` `// Driver Code` ` ` ` ` `let N = 15;` ` ` `let K = 2;` ` ` `countNumberHavingKthBitSet(N, K);` ` ` `</script>` |

**Output:**

4

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.