给定两个正整数N和K ,任务是对范围[1,N]中的数字进行计数,其范围从右起的第K位,即LSB,是最右边的设置位。
例子:
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
天真的方法:想法是在[1,N]范围内进行迭代,并对该范围内的每个数字检查最右边的设置位的位置是否为K并打印这些数字的计数。
时间复杂度: O(N LogN)
辅助空间: O(1)
高效的方法:想法是在每一步中找到位置最靠右的位置i处的数字。请按照以下步骤解决问题:
- 使用变量i遍历范围[1,K] 。
- 找出最右边的设置位是i th的数字。
- 从N减去该数字。
- 对i的所有值重复上述步骤。
下面是上述方法的实现:
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
// 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
输出:
4
时间复杂度: O(K)
辅助空间: O(1)