Skip to content
Related Articles

Related Articles

Maximize the number by flipping at most K bits

View Discussion
Improve Article
Save Article
Like Article
  • 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 <bits/stdc++.h>
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




<script>
// Javascript implementation of the approach
 
 
// Function to convert decimal number n
// to its binary representation
// stored as an array arr[]
function decBinary(arr, n) {
    let k = Math.log2(n);
    while (n > 0) {
        arr[k--] = n % 2;
        n = Math.floor(n / 2);
    }
}
 
// Function to convert the number
// represented as a binary array
// arr[] into its decimal equivalent
function binaryDec(arr, n) {
    let ans = 0;
    for (let i = 0; i < n; i++)
        ans += arr[i] << (n - i - 1);
    return ans;
}
 
// Function to return the maximized
// number by flipping atmost k bits
function maxNum(n, k) {
 
    // Number of bits in n
    let l = Math.log2(n) + 1;
 
    // Find the binary representation of n
    let a = new Array(l).fill(0);
    decBinary(a, n);
 
    // To count the number of 0s flipped
    let cn = 0;
    for (let 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
 
let n = 4, k = 1;
 
document.write(maxNum(n, k));
 
// This code is contributed by _saurabh_jaiswal.
</script>

Output: 

6

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!