连续整数的最小计数,直到 N 其按位与为 0 与 N
给定一个正整数N ,任务是打印小于N的连续数字的最小计数,使得这些连续元素(包括整数N )的按位与等于0 。
例子:
Input: N = 18
Output: 3
Explanation:
One possible way is to form a sequence of {15, 16, 17, and 18}. The bitwise AND of the given numbers is equal to 0.
Therefore, a minimum of 3 numbers are needed to make the bitwise AND of a sequence of 4 consecutive elements, including 18 to 0.
Input: N = 4
Output: 1
Explanation:
One possible way is to form a sequence of {4, 3}. The bitwise AND of the given numbers is equal to 0.
Therefore, a minimum of 1 number is needed to make the bitwise AND of a sequence of 2 consecutive elements including 4 to 0.
朴素方法:最简单的方法是迭代直到N大于0 ,并在每次迭代中检查到目前为止的数字的按位与是否等于0 。如果不是,则将计数增加1 。
时间复杂度: O(N)
辅助空间: O(1)
有效方法:可以根据以下观察解决给定的问题:
- 为了使包括N的序列的按位与等于0 ,需要使数N的MSB位等于0 。
- 因此,想法是在序列中包含所有大于或等于(2 MSB -1)且小于N的整数,它将给出最小计数。
请按照以下步骤解决问题:
- 找到整数N的最高有效位并将其存储在变量m中。
- 那么将包含的最大最小值是(2 m -1) 。
- 最后,将计数打印为(N- (2 m -1)) 。
下面是上述方法的实现:
C++
// C++ Program for the above approach
#include
using namespace std;
int decimalToBinary(int N)
{
// To store the binary number
int B_Number = 0;
int cnt = 0;
while (N != 0)
{
int rem = N % 2;
double c = pow(10, cnt);
B_Number += rem * c;
N /= 2;
// Count used to store exponent value
cnt++;
}
return B_Number;
}
// Function to count the minimum count of
// integers such that bitwise AND of that
// many consecutive elements is equal to 0
int count(int N)
{
// Stores the binary
// representation of N
string a = to_string(decimalToBinary(N));
// Stores the MSB bit
int m = a.size() - 1;
// Stores the count
// of numbers
int res = (N - (pow(2, m) - 1));
// Return res
return res;
}
// Driver Code
int main() {
// Given Input
int N = 18;
// Function Call
cout<< count(N);
return 0;
}
// This code is contributed by shikhasingrajput
Java
// Java program for the above approach
class GFG
{
// Function to count the minimum count of
// integers such that bitwise AND of that
// many consecutive elements is equal to 0
static int count(int N)
{
// Stores the binary
// representation of N
String a = Integer.toBinaryString(N);
// Stores the MSB bit
int m = a.length() - 1;
// Stores the count
// of numbers
int res = (int) (N - (Math.pow(2, m) - 1));
// Return res
return res;
}
// Driver Code
public static void main(String[] args) {
// Given Input
int N = 18;
// Function Call
System.out.println(count(N));
}
}
// This code is contributed by shikhasingrajput
Python3
# Python program for the above approach
# Function to count the minimum count of
# integers such that bitwise AND of that
# many consecutive elements is equal to 0
def count(N):
# Stores the binary
# representation of N
a = bin(N)
# Excludes first two
# characters "0b"
a = a[2:]
# Stores the MSB bit
m = len(a)-1
# Stores the count
# of numbers
res = N - (2**m-1)
# Return res
return res
# Driver Code
# Given Input
N = 18
# Function Call
print(count(N))
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Function to count the minimum count of
// integers such that bitwise AND of that
// many consecutive elements is equal to 0
static int count(int N)
{
// Stores the binary
// representation of N
String a = Convert.ToString(N, 2);
// Stores the MSB bit
int m = a.Length - 1;
// Stores the count
// of numbers
int res = (int) (N - (Math.Pow(2, m) - 1));
// Return res
return res;
}
// Driver Code
public static void Main(String[] args) {
// Given Input
int N = 18;
// Function Call
Console.WriteLine(count(N));
}
}
// This code is contributed by umadevi9616
Javascript
3
时间复杂度: O(log(N))
辅助空间: O(1)