给定一个可以用32位表示的整数N ,任务是找到另一个整数X ,该整数X的二进制表示形式最多设置K个位,并且X和N的按位与最大。
例子:
Input: N = 5, K = 1
Output: X = 2
Explanation:
Binary representation of 5 is 101, the possible value of X such that it maximize the AND is 2
5 -> 101
2 -> 100
AND sum -> 100 (2)
4 is the maximum AND sum we can get with N and X.
Input: N = 10, K = 2
Output: X = 10
Explanation:
Binary representation of 10 is 1010, X = 10 possible integer to given maximum AND sum with N with atmost 2 bit set.
10 -> 1010
10 -> 1010
AND sum -> 1010 (10)
10 is the maximum AND sum we can get with N and X.
幼稚的方法:幼稚的解决方案是运行一个从1到N的循环,并对N进行按位与,并检查设置的位数是否小于或等于K。在每次迭代期间,请保持按位与的最大值。
时间复杂度: O(N)
高效方法:通过对位使用贪婪方法,可以有效解决此问题。由于N最多可以有32位。因此,我们将开始从最高有效位开始遍历,并检查是否在N中将其置位(或为1),如果未将其置位,则无需设置该位,因为在最终答案中,AND操作会将其设置为零,否则我们将在所需的答案中进行设置。此外,我们将在每次迭代中维护设置位的计数,并检查其是否不超过K。
时间复杂度: O(32)
下面是上述有效方法的实现:
C++
// C++ program for the
// above approach
#include
using namespace std;
// Function to find
// the integer with
// maximum bitwise with N
int find_max(int n, int k)
{
// Store answer in the bitset
// Initialized with 0
bitset<32> X(0);
// To maintain the count
// of set bits that should
// exceed k
int cnt = 0;
// Start traversing from the
// Most significantif that bit
// is set in n then we will set
// in our answer i.e in X
for (int i = 31; i >= 0 &&
cnt != k; i--)
{
// Checking if the ith bit
// is set in n or not
if (n & (1 << i))
{
X[i] = 1;
cnt++;
}
}
// Converting into integer
return X.to_ulong();
}
// Driver Code
int main()
{
int n = 10, k = 2;
// Function Call
cout << find_max(n, k) <<
endl;
return 0;
}
Java
// Java program for the
// above approach
import java.util.*;
import java.lang.*;
class GFG{
// Function to find
// the integer with
// maximum bitwise with N
static int find_max(int n,
int k)
{
// Store answer in the
// bitset Initialized
// with 0
int[] X = new int[32];
// To maintain the count
// of set bits that should
// exceed k
int cnt = 0;
// Start traversing from the
// Most significant if that bit
// is set in n then we will set
// in our answer i.e in X
for (int i = 31; i >= 0 &&
cnt != k; i--)
{
// Checking if the ith bit
// is set in n or not
if ((n & (1 << i)) != 0)
{
X[i] = 1;
cnt++;
}
}
String s = "";
for(int i = 31; i >= 0; i--)
s += X[i] == 0 ? '0' : '1';
// Converting into integer
return Integer.parseInt(s,2);
}
// Driver function
public static void main (String[] args)
{
int n = 10, k = 2;
// Function Call
System.out.println(find_max(n, k));
}
}
// This code is contributed by offbeat
Python3
# Python3 program for the above approach
# Function to find the integer with
# maximum bitwise with N
def find_max(n, k):
# Store answer in the
# bitset Initialized
# with 0
X = [0] * 32
# To maintain the count
# of set bits that should
# exceed k
cnt = 0
# Start traversing from the
# Most significant if that bit
# is set in n then we will set
# in our answer i.e in X
i = 31
while (i >= 0 and cnt != k):
# Checking if the ith bit
# is set in n or not
if ((n & (1 << i)) != 0):
X[i] = 1
cnt += 1
i -= 1
s = ""
for i in range(31, -1, -1):
if X[i] == 0:
s += '0'
else:
s += '1'
# Converting into integer
return int(s, 2)
# Driver code
n, k = 10, 2
# Function Call
print(find_max(n, k))
# This code is contributed by divyeshrabadiya07
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the integer with
// maximum bitwise with N
static int find_max(int n, int k)
{
// Store answer in the
// bitset Initialized
// with 0
int[] X = new int[32];
// To maintain the count
// of set bits that should
// exceed k
int cnt = 0;
// Start traversing from the
// Most significant if that bit
// is set in n then we will set
// in our answer i.e in X
for(int i = 31; i >= 0 && cnt != k; i--)
{
// Checking if the ith bit
// is set in n or not
if ((n & (1 << i)) != 0)
{
X[i] = 1;
cnt++;
}
}
String s = "";
for(int i = 31; i >= 0; i--)
s += X[i] == 0 ? '0' : '1';
// Converting into integer
return Convert.ToInt32(s, 2);
}
// Driver code
public static void Main(String[] args)
{
int n = 10, k = 2;
// Function Call
Console.Write(find_max(n, k));
}
}
// This code is contributed by offbeat
Javascript
10
性能分析:
- 时间复杂度: O(32)
- 辅助空间: O(1