Minimum integer with at most K bits set such that their bitwise AND with N is maximum
Last Updated :
07 Apr, 2021
Given an integer N which can be represented in 32 bit, the task is to find another integer X that has at most K bits set in its binary representation and bitwise AND of X and N is maximum.
Examples:
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.
Naive Approach: A naive solution is to run a loop from 1 to N and take bitwise AND with N, and also check if the number of set bits is less than or equal to K. During each iteration maintain the maximum value of bitwise AND.
Time Complexity: O(N)
Efficient Approach: This problem can be solved efficiently by using a Greedy approach on bits. Since N can have at most 32 bits. So we will start traversing from the Most Significant bit and check if it is set(or 1) in N, if it is not set then there is no need to set this bit because AND operation will make it zero in the final answer, else we will set it in our required answer. Also, we will maintain the count of set bits in each iteration and check if it does not exceed K.
Time Complexity: O(32)
Below is the implementation of the above efficient approach :
C++
#include <bits/stdc++.h>
using namespace std;
int find_max( int n, int k)
{
bitset<32> X(0);
int cnt = 0;
for ( int i = 31; i >= 0 &&
cnt != k; i--)
{
if (n & (1 << i))
{
X[i] = 1;
cnt++;
}
}
return X.to_ulong();
}
int main()
{
int n = 10, k = 2;
cout << find_max(n, k) <<
endl;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG{
static int find_max( int n,
int k)
{
int [] X = new int [ 32 ];
int cnt = 0 ;
for ( int i = 31 ; i >= 0 &&
cnt != k; i--)
{
if ((n & ( 1 << i)) != 0 )
{
X[i] = 1 ;
cnt++;
}
}
String s = "" ;
for ( int i = 31 ; i >= 0 ; i--)
s += X[i] == 0 ? '0' : '1' ;
return Integer.parseInt(s, 2 );
}
public static void main (String[] args)
{
int n = 10 , k = 2 ;
System.out.println(find_max(n, k));
}
}
|
Python3
def find_max(n, k):
X = [ 0 ] * 32
cnt = 0
i = 31
while (i > = 0 and cnt ! = k):
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'
return int (s, 2 )
n, k = 10 , 2
print (find_max(n, k))
|
C#
using System;
class GFG{
static int find_max( int n, int k)
{
int [] X = new int [32];
int cnt = 0;
for ( int i = 31; i >= 0 && cnt != k; i--)
{
if ((n & (1 << i)) != 0)
{
X[i] = 1;
cnt++;
}
}
String s = "" ;
for ( int i = 31; i >= 0; i--)
s += X[i] == 0 ? '0' : '1' ;
return Convert.ToInt32(s, 2);
}
public static void Main(String[] args)
{
int n = 10, k = 2;
Console.Write(find_max(n, k));
}
}
|
Javascript
<script>
function find_max(n, k)
{
var X = Array.from({length: 32}, (_, i) => 0);
var cnt = 0;
for (i = 31; i >= 0 &&
cnt != k; i--)
{
if ((n & (1 << i)) != 0)
{
X[i] = 1;
cnt++;
}
}
var s = "" ;
for (i = 31; i >= 0; i--)
s += X[i] == 0 ? '0' : '1' ;
return parseInt(s,2);
}
var n = 10, k = 2;
document.write(find_max(n, k));
</script>
|
Performance Analysis:
- Time complexity: O(32)
- Auxiliary Space: O(1
Share your thoughts in the comments
Please Login to comment...