📌  相关文章
📜  通过最多翻转K位来最大化数量

📅  最后修改于: 2021-05-25 05:42:28             🧑  作者: Mango

给定整数N ,任务是找到可以通过翻转N的二进制表示形式中的最多K个位获得的最大数字。

例子:

方法:

  • 如果N的二进制表示形式中的0数目小于K,则将所有0翻转。
  • 如果0的数量大于或等于K,则翻转最高有效的K 0s
  • 最后,打印最大化的整数。

下面是上述方法的实现:

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


输出:
6