在二进制表示中 LSB 为 0 的范围 [L, R] 中的数字计数
给定两个整数L和R 。任务是找出范围[L, R]中所有二进制表示的最低有效位为0 的数字的计数。
例子:
Input: L = 10, R = 20
Output: 6
Input: L = 7, R = 11
Output: 2
天真的方法:解决此问题的最简单方法是检查范围[L, R] 中的每个数字,如果二进制表示中的最低有效位为0。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the count
// of required numbers
int countNumbers(int l, int r)
{
int count = 0;
for (int i = l; i <= r; i++) {
// If rightmost bit is 0
if ((i & 1) == 0) {
count++;
}
}
// Return the required count
return count;
}
// Driver code
int main()
{
int l = 10, r = 20;
// Call function countNumbers
cout << countNumbers(l, r);
return 0;
}
Java
// Java implementation of the approach
import java.io.*;
class GFG{
// Function to return the count
// of required numbers
static int countNumbers(int l, int r)
{
int count = 0;
for(int i = l; i <= r; i++)
{
// If rightmost bit is 0
if ((i & 1) == 0)
count += 1;
}
// Return the required count
return count;
}
// Driver code
public static void main(String[] args)
{
int l = 10, r = 20;
// Call function countNumbers
System.out.println(countNumbers(l, r));
}
}
// This code is contributed by MuskanKalra1
Python3
# Python3 implementation of the approach
# Function to return the count
# of required numbers
def countNumbers(l, r):
count = 0
for i in range(l, r + 1):
# If rightmost bit is 0
if ((i & 1) == 0):
count += 1
# Return the required count
return count
# Driver code
l = 10
r = 20
# Call function countNumbers
print(countNumbers(l, r))
# This code is contributed by amreshkumar3
C#
// C# implementation of the approach
using System;
class GFG {
// Function to return the count
// of required numbers
static int countNumbers(int l, int r)
{
int count = 0;
for (int i = l; i <= r; i++) {
// If rightmost bit is 0
if ((i & 1) == 0)
count += 1;
}
// Return the required count
return count;
}
// Driver code
public static void Main()
{
int l = 10, r = 20;
// Call function countNumbers
Console.WriteLine(countNumbers(l, r));
}
}
// This code is contributed by subham348.
Javascript
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the count
// of required numbers
int countNumbers(int l, int r)
{
// Count of numbers in range
// which are divisible by 2
return ((r / 2) - (l - 1) / 2);
}
// Driver code
int main()
{
int l = 10, r = 20;
cout << countNumbers(l, r);
return 0;
}
Java
// Java implementation of the approach
import java.io.*;
class GFG{
// Function to return the count
// of required numbers
static int countNumbers(int l, int r)
{
// Count of numbers in range
// which are divisible by 2
return ((r / 2) - (l - 1) / 2);
}
// Driver Code
public static void main(String[] args)
{
int l = 10;
int r = 20;
System.out.println(countNumbers(l, r));
}
}
// This code is contributed by MuskanKalra1
Python3
# Python3 implementation of the approach
# Function to return the count
# of required numbers
def countNumbers(l, r):
# Count of numbers in range
# which are divisible by 2
return ((r // 2) - (l - 1) // 2)
# Driver code
l = 10
r = 20
print(countNumbers(l, r))
# This code is contributed by amreshkumar3
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG{
// Function to return the count
// of required numbers
static int countNumbers(int l, int r)
{
// Count of numbers in range
// which are divisible by 2
return ((r / 2) - (l - 1) / 2);
}
// Driver code
public static void Main()
{
int l = 10, r = 20;
Console.Write(countNumbers(l, r));
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出
6
时间复杂度: O(r – l)
辅助空间: O(1)
有效的方法:这个问题可以通过使用比特的属性来解决。只有偶数的最右边位为0 。可以使用此公式((R / 2) – (L – 1) / 2)在O(1)时间内找到计数。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the count
// of required numbers
int countNumbers(int l, int r)
{
// Count of numbers in range
// which are divisible by 2
return ((r / 2) - (l - 1) / 2);
}
// Driver code
int main()
{
int l = 10, r = 20;
cout << countNumbers(l, r);
return 0;
}
Java
// Java implementation of the approach
import java.io.*;
class GFG{
// Function to return the count
// of required numbers
static int countNumbers(int l, int r)
{
// Count of numbers in range
// which are divisible by 2
return ((r / 2) - (l - 1) / 2);
}
// Driver Code
public static void main(String[] args)
{
int l = 10;
int r = 20;
System.out.println(countNumbers(l, r));
}
}
// This code is contributed by MuskanKalra1
Python3
# Python3 implementation of the approach
# Function to return the count
# of required numbers
def countNumbers(l, r):
# Count of numbers in range
# which are divisible by 2
return ((r // 2) - (l - 1) // 2)
# Driver code
l = 10
r = 20
print(countNumbers(l, r))
# This code is contributed by amreshkumar3
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG{
// Function to return the count
// of required numbers
static int countNumbers(int l, int r)
{
// Count of numbers in range
// which are divisible by 2
return ((r / 2) - (l - 1) / 2);
}
// Driver code
public static void Main()
{
int l = 10, r = 20;
Console.Write(countNumbers(l, r));
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出
6
时间复杂度: O(1)
辅助空间: O(1)