Given two numbers N and K, The task is to find the index of the K-th set bit in the number from the right.
Note: Indexing in the binary representation starts from 0 from the right. For example in the binary number “000011”, the first set bit is at index 0 from the right, and the second set bit is at index 1 from the right.
Examples:
Input: N = 15, K = 3
Output: 2
15 is "1111", hence the third bit is at index 2 from right.
Input: N = 19, K = 2
Output: 1
19 is "10011", hence the second set bit is at index 1 from right.
Approach: Initialize a counter 0, and increase it if the last bit is set in the number. For accessing the next bit, right-shift the number by 1. When the counter’s value is equal to K, then we return the index of the number which was being incremented on every right shift.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int FindIndexKthBit( int n, int k)
{
int cnt = 0;
int ind = 0;
while (n) {
if (n & 1)
cnt++;
if (cnt == k)
return ind;
ind++;
n = n >> 1;
}
return -1;
}
int main()
{
int n = 15, k = 3;
int ans = FindIndexKthBit(n, k);
if (ans != -1)
cout << ans;
else
cout << "No k-th set bit" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int FindIndexKthBit( int n, int k)
{
int cnt = 0 ;
int ind = 0 ;
while (n > 0 )
{
if ((n & 1 ) != 0 )
cnt++;
if (cnt == k)
return ind;
ind++;
n = n >> 1 ;
}
return - 1 ;
}
public static void main(String args[])
{
int n = 15 , k = 3 ;
int ans = FindIndexKthBit(n, k);
if (ans != - 1 )
System.out.println(ans);
else
System.out.println( "No k-th set bit" );
}
}
|
Python3
def FindIndexKthBit(n, k):
cnt, ind = 0 , 0
while n > 0 :
if n & 1 :
cnt + = 1
if cnt = = k:
return ind
ind + = 1
n = n >> 1
return - 1
if __name__ = = "__main__" :
n, k = 15 , 3
ans = FindIndexKthBit(n, k)
if ans ! = - 1 :
print (ans)
else :
print ( "No k-th set bit" )
|
C#
using System;
class GFG
{
static int FindIndexKthBit( int n, int k)
{
int cnt = 0;
int ind = 0;
while (n > 0)
{
if ((n & 1 ) != 0)
cnt++;
if (cnt == k)
return ind;
ind++;
n = n >> 1;
}
return -1;
}
public static void Main()
{
int n = 15, k = 3;
int ans = FindIndexKthBit(n, k);
if (ans != -1)
Console.WriteLine(ans);
else
Console.WriteLine( "No k-th set bit" );
}
}
|
PHP
<?php
function FindIndexKthBit( $n , $k )
{
$cnt = 0;
$ind = 0;
while ( $n )
{
if ( $n & 1)
$cnt ++;
if ( $cnt == $k )
return $ind ;
$ind ++;
$n = $n >> 1;
}
return -1;
}
$n = 15;
$k = 3;
$ans = FindIndexKthBit( $n , $k );
if ( $ans != -1)
echo $ans ;
else
echo "No k-th set bit" ;
?>
|
Javascript
<script>
function FindIndexKthBit(n, k)
{
let cnt = 0;
let ind = 0;
while (n > 0) {
if (n & 1 > 0)
cnt++;
if (cnt == k)
return ind;
ind++;
n = n >> 1;
}
return -1;
}
let n = 15, k = 3;
let ans = FindIndexKthBit(n, k);
if (ans != -1)
document.write(ans);
else
document.write( "No k-th set bit" );
</script>
|
Time Complexity: O(logN), as we are using a while loop to traverse and in each traversal we are right shifting N by 1 place which is equivalent to floor division of N by 2. So, the effective time will be 1+1/2+1/4+…..+1/2^N which is equivalent to logN.
Auxiliary Space: O(1), as we are not using any extra space.
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!