# Maximize the number by flipping at most K bits

• Last Updated : 11 Jun, 2021

Given an integer N, the task is to find the greatest number that can be obtained by flipping at most K bits in the binary representation of N.
Examples:

Input: N = 4, K = 1
Output:
The binary equivalent of 4 is 100.
On flipping the 1st 0, we get 110
which is equivalent to 6.
Input: N = 5, K = 2
Output:

Approach:

• If the number of 0s in the binary representation of N is less than K then flip all the 0s.
• If the number of 0s is greater than or equal to K then flip the most significant K 0s.
• Finally, print the maximized integer.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function to convert decimal number n``// to its binary representation``// stored as an array arr[]``void` `decBinary(``int` `arr[], ``int` `n)``{``    ``int` `k = log2(n);``    ``while` `(n > 0) {``        ``arr[k--] = n % 2;``        ``n /= 2;``    ``}``}` `// Function to convert the number``// represented as a binary array``// arr[] into its decimal equivalent``int` `binaryDec(``int` `arr[], ``int` `n)``{``    ``int` `ans = 0;``    ``for` `(``int` `i = 0; i < n; i++)``        ``ans += arr[i] << (n - i - 1);``    ``return` `ans;``}` `// Function to return the maximized``// number by flipping atmost k bits``int` `maxNum(``int` `n, ``int` `k)``{` `    ``// Number of bits in n``    ``int` `l = log2(n) + 1;` `    ``// Find the binary representation of n``    ``int` `a[l] = { 0 };``    ``decBinary(a, n);` `    ``// To count the number of 0s flipped``    ``int` `cn = 0;``    ``for` `(``int` `i = 0; i < l; i++) {``        ``if` `(a[i] == 0 && cn < k) {``            ``a[i] = 1;``            ``cn++;``        ``}``    ``}` `    ``// Return the decimal equivalent``    ``// of the maximized number``    ``return` `binaryDec(a, l);``}` `// Driver code``int` `main()``{``    ``int` `n = 4, k = 1;` `    ``cout << maxNum(n, k);` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``class` `GFG``{``    ` `    ``// Function to convert decimal number n``    ``// to its binary representation``    ``// stored as an array arr[]``    ``static` `void` `decBinary(``int` `arr[], ``int` `n)``    ``{``        ``int` `k = (``int``)(Math.log(n) /``                      ``Math.log(``2``));``        ` `        ``while` `(n > ``0``)``        ``{``            ``arr[k--] = n % ``2``;``            ``n /= ``2``;``        ``}``    ``}``    ` `    ``// Function to convert the number``    ``// represented as a binary array``    ``// arr[] into its decimal equivalent``    ``static` `int` `binaryDec(``int` `arr[], ``int` `n)``    ``{``        ``int` `ans = ``0``;``        ``for` `(``int` `i = ``0``; i < n; i++)``            ``ans += arr[i] << (n - i - ``1``);``        ``return` `ans;``    ``}``    ` `    ``// Function to return the maximized``    ``// number by flipping atmost k bits``    ``static` `int` `maxNum(``int` `n, ``int` `k)``    ``{``    ` `        ``// Number of bits in n``        ``int` `l = (``int``)(Math.log(n) /``                      ``Math.log(``2``)) + ``1``;``    ` `        ``// Find the binary representation of n``        ``int` `a[] = ``new` `int``[l];``        ``decBinary(a, n);``    ` `        ``// To count the number of 0s flipped``        ``int` `cn = ``0``;``        ``for` `(``int` `i = ``0``; i < l; i++)``        ``{``            ``if` `(a[i] == ``0` `&& cn < k)``            ``{``                ``a[i] = ``1``;``                ``cn++;``            ``}``        ``}``    ` `        ``// Return the decimal equivalent``        ``// of the maximized number``        ``return` `binaryDec(a, l);``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `main (String[] args)``    ``{``        ``int` `n = ``4``, k = ``1``;``    ` `        ``System.out.println(maxNum(n, k));``    ``}``}` `// This code is contributed by AnkitRai01`

## Python3

 `# Python implementation of the approach` `import` `math` `# Function to convert decimal number n``# to its binary representation``# stored as an array arr[]``def` `decBinary(arr, n):``    ``k ``=` `int``(math.log2(n))``    ``while` `(n > ``0``):``        ``arr[k] ``=` `n ``%` `2``        ``k ``=` `k ``-` `1``        ``n ``=` `n``/``/``2` `# Function to convert the number``# represented as a binary array``# arr[] into its decimal equivalent``def` `binaryDec(arr, n):``    ``ans ``=` `0``    ``for` `i ``in` `range``(``0``, n):``        ``ans ``=` `ans ``+` `(arr[i] << (n ``-` `i ``-` `1``))``    ``return` `ans` `# Function to return the maximized``# number by flipping atmost k bits``def` `maxNum(n, k):``    ` `    ``# Number of bits in n``    ``l ``=` `int``(math.log2(n)) ``+` `1` `    ``# Find the binary representation of n``    ``a ``=` `[``0` `for` `i ``in` `range``(``0``, l)]``    ``decBinary(a, n)` `    ``# To count the number of 0s flipped``    ``cn ``=` `0``    ``for` `i ``in` `range``(``0``, l):``        ``if` `(a[i] ``=``=` `0` `and` `cn < k):``            ``a[i] ``=` `1``            ``cn ``=` `cn ``+` `1``            ` `    ``# Return the decimal equivalent``    ``# of the maximized number``    ``return` `binaryDec(a, l)` `# Driver code``n ``=` `4``k ``=` `1` `print``(maxNum(n, k))` `# This code is contributed by Sanjit_Prasad`

## C#

 `// C# implementation of the approach``using` `System;` `class` `GFG``{``    ` `    ``// Function to convert decimal number n``    ``// to its binary representation``    ``// stored as an array []arr``    ``static` `void` `decBinary(``int` `[]arr, ``int` `n)``    ``{``        ``int` `k = (``int``)(Math.Log(n) /``                      ``Math.Log(2));``        ` `        ``while` `(n > 0)``        ``{``            ``arr[k--] = n % 2;``            ``n /= 2;``        ``}``    ``}``    ` `    ``// Function to convert the number``    ``// represented as a binary array``    ``// []arr into its decimal equivalent``    ``static` `int` `binaryDec(``int` `[]arr, ``int` `n)``    ``{``        ``int` `ans = 0;``        ``for` `(``int` `i = 0; i < n; i++)``            ``ans += arr[i] << (n - i - 1);``        ``return` `ans;``    ``}``    ` `    ``// Function to return the maximized``    ``// number by flipping atmost k bits``    ``static` `int` `maxNum(``int` `n, ``int` `k)``    ``{``    ` `        ``// Number of bits in n``        ``int` `l = (``int``)(Math.Log(n) /``                      ``Math.Log(2)) + 1;``    ` `        ``// Find the binary representation of n``        ``int` `[]a = ``new` `int``[l];``        ``decBinary(a, n);``    ` `        ``// To count the number of 0s flipped``        ``int` `cn = 0;``        ``for` `(``int` `i = 0; i < l; i++)``        ``{``            ``if` `(a[i] == 0 && cn < k)``            ``{``                ``a[i] = 1;``                ``cn++;``            ``}``        ``}``    ` `        ``// Return the decimal equivalent``        ``// of the maximized number``        ``return` `binaryDec(a, l);``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `Main(String[] args)``    ``{``        ``int` `n = 4, k = 1;``    ` `        ``Console.WriteLine(maxNum(n, k));``    ``}``}` `// This code is contributed by Rajput-Ji`

## Javascript

 ``

Output:

`6`

My Personal Notes arrow_drop_up