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++
#include <bits/stdc++.h>
using namespace std;
int countNumberHavingKthBitSet( int N, int K)
{
int numbers_rightmost_setbit_K;
for ( int i = 1; i <= K; i++) {
int numbers_rightmost_bit_i = (N + 1) / 2;
N -= numbers_rightmost_bit_i;
if (i == K) {
numbers_rightmost_setbit_K
= numbers_rightmost_bit_i;
}
}
cout << numbers_rightmost_setbit_K;
}
int main()
{
int N = 15;
int K = 2;
countNumberHavingKthBitSet(N, K);
return 0;
}
|
Java
import java.util.Arrays;
class GFG
{
static void countNumberHavingKthBitSet( int N, int K)
{
int numbers_rightmost_setbit_K = 0 ;
for ( int i = 1 ; i <= K; i++)
{
int numbers_rightmost_bit_i = (N + 1 ) / 2 ;
N -= numbers_rightmost_bit_i;
if (i == K)
{
numbers_rightmost_setbit_K
= numbers_rightmost_bit_i;
}
}
System.out.println(numbers_rightmost_setbit_K);
}
static public void main(String args[])
{
int N = 15 ;
int K = 2 ;
countNumberHavingKthBitSet(N, K);
}
}
|
Python3
def countNumberHavingKthBitSet(N, K):
numbers_rightmost_setbit_K = 0
for i in range ( 1 , K + 1 ):
numbers_rightmost_bit_i = (N + 1 ) / / 2
N - = numbers_rightmost_bit_i
if (i = = K):
numbers_rightmost_setbit_K = numbers_rightmost_bit_i
print (numbers_rightmost_setbit_K)
if __name__ = = '__main__' :
N = 15
K = 2
countNumberHavingKthBitSet(N, K)
|
C#
using System;
class GFG
{
static void countNumberHavingKthBitSet( int N, int K)
{
int numbers_rightmost_setbit_K = 0;
for ( int i = 1; i <= K; i++)
{
int numbers_rightmost_bit_i = (N + 1) / 2;
N -= numbers_rightmost_bit_i;
if (i == K)
{
numbers_rightmost_setbit_K
= numbers_rightmost_bit_i;
}
}
Console.WriteLine(numbers_rightmost_setbit_K);
}
static public void Main(String []args)
{
int N = 15;
int K = 2;
countNumberHavingKthBitSet(N, K);
}
}
|
Javascript
<script>
function countNumberHavingKthBitSet(N, K)
{
let numbers_rightmost_setbit_K = 0;
for (let i = 1; i <= K; i++)
{
let numbers_rightmost_bit_i = (N + 1) / 2;
N -= numbers_rightmost_bit_i;
if (i == K)
{
numbers_rightmost_setbit_K
= numbers_rightmost_bit_i;
}
}
document.write(numbers_rightmost_setbit_K);
}
let N = 15;
let K = 2;
countNumberHavingKthBitSet(N, K);
</script>
|
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!