# Minimum integer with at most K bits set such that their bitwise AND with N is maximum

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 = 1Output:X = 2Explanation:

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 = 2Output:X = 10Explanation:

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++

`// C++ program for the` `// above approach` `#include <bits/stdc++.h>` `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

`<script>` `// javascript program for the` `// above approach` `// Function to find` `// the integer with` `// maximum bitwise with N` `function` `find_max(n, k)` `{` ` ` `// Store answer in the` ` ` `// bitset Initialized` ` ` `// with 0` ` ` `var` `X = Array.from({length: 32}, (_, i) => 0);` ` ` `// To maintain the count` ` ` `// of set bits that should` ` ` `// exceed k` ` ` `var` `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` `(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++;` ` ` `}` ` ` `}` ` ` `var` `s = ` `""` `;` ` ` `for` `(i = 31; i >= 0; i--)` ` ` `s += X[i] == 0 ? ` `'0'` `: ` `'1'` `;` ` ` `// Converting into integer` ` ` `return` `parseInt(s,2);` `}` `// Driver function` `var` `n = 10, k = 2;` `// Function Call` `document.write(find_max(n, k));` `// This code is contributed by Princi Singh` `</script>` |

**Output**

10

**Performance Analysis:**

**Time complexity:**O(32)**Auxiliary Space:**O(1