📌  相关文章
📜  用k个设置位最大化一个数字所需的最小翻转

📅  最后修改于: 2021-04-23 20:22:15             🧑  作者: Mango

给定两个数字n和k,我们需要通过翻转位来找到使给定数最大化所需的最小翻转次数,以使结果数恰好具有k个设置位。
注意:K必须小于n中的位数。

例子 :

Input : n = 14, k = 2
Output : Min Flips = 1
Explanation : 
Binary representation of 14 = 1110
Largest 4-digit Binary number with
2 set bit = 1100
Conversion from 1110 to 1100 
requires 1 flipping

Input : n = 145, k = 4
Output : Min Flips = 3
Explanation : 
Binary representation of 145 = 10010001
Largest 8-digit Binary number with 
4 set bit = 11110000
Conversion from 10010001 to 11110000 
requires 3 flipping

对于给定的数字n和k,找到具有k个置位的位数并且与n完全相同的位数的最大数为:

  • size = log2(n)+1给出n的位数。
  • max = pow(2,k)– 1给出k位最大可能数。
  • max = max <<(size – k)给出k个设置位可能的最大数量,并且位数与n完全相同
  • (n XOR max)中的置位位数是我们所需的翻转次数。

上述方法的说明:

let n = 145 (10010001), k = 4

size = log2(n) + 1 = log2(145) + 1 
                   = 7 + 1 = 8 

max = pow(2, k) -1 = pow(2, 4) - 1 
                   = 16 - 1 = 15 (1111) 

max = max << (size - k) = 15 << (8 - 4) 
                        = 240 (11110000)

number of set bit in  =  no. of set bit in 
(n XOR max )              (145 ^ 240 ) 
                      = 3
C++
// CPP for finding min flip
// for maximizing given n
#include 
using namespace std;
  
// function for finding set bit
int setBit(int xorValue)
{
    int count = 0;
    while (xorValue) {
        if (xorValue % 2)
            count++;
  
        xorValue /= 2;
    }
      
    // return count of set bit
    return count;
}
  
// function for finding min flip
int minFlip(int n, int k)
{   
    // number of bits in n
    int size = log2(n) + 1;
      
    // Find the largest number of
    // same size with k set bits
    int max = pow(2, k) - 1;    
    max = max << (size - k);
  
    // Count bit differences to find
    // required flipping.
    int xorValue = (n ^ max);
    return (setBit(xorValue));
}
  
// driver program
int main()
{
    int n = 27, k = 3;
    cout << "Min Flips = " << minFlip(n, k);
    return 0;
}


Java
// JAVA Code to find Minimum flips required
// to maximize a number with k set bits
import java.util.*;
  
class GFG {
      
    // function for finding set bit
    static int setBit(int xorValue)
    {
        int count = 0;
        while (xorValue >= 1) {
            if (xorValue % 2 == 1)
                count++;
       
            xorValue /= 2;
        }
           
        // return count of set bit
        return count;
    }
       
    // function for finding min flip
    static int minFlip(int n, int k)
    {   
        // number of bits in n
        int size = (int)(Math.log(n) / 
                         Math.log(2)) + 1;
           
        // Find the largest number of
        // same size with k set bits
        int max = (int)Math.pow(2, k) - 1;    
        max = max << (size - k);
       
        // Count bit differences to find
        // required flipping.
        int xorValue = (n ^ max);
        return (setBit(xorValue));
    }
      
    /* Driver program to test above function */
    public static void main(String[] args) 
    {
         int n = 27, k = 3;
         System.out.println("Min Flips = "+
                             minFlip(n, k));
    }
}
  
// This code is contributed by Arnav Kr. Mandal.


Python 3
# Python3 for finding min flip
# for maximizing given n
import math
  
# function for finding set bit
def setBit(xorValue):
  
    count = 0
    while (xorValue):
        if (xorValue % 2):
            count += 1
          
        xorValue = int(xorValue / 2)
          
    # return count 
    # of set bit
    return count
  
# function for 
# finding min flip
def minFlip(n, k):
  
    # number of bits in n
    size = int(math.log(n) / 
               math.log(2) + 1)
      
    # Find the largest number of
    # same size with k set bits
    max = pow(2, k) - 1
    max = max << (size - k)
  
    # Count bit differences to 
    # find required flipping.
    xorValue = (n ^ max)
    return (setBit(xorValue))
  
# Driver Code
n = 27
k = 3
print("Min Flips = " , 
        minFlip(n, k))
  
# This code is contributed
# by Smitha


C#
// C# Code to find Minimum flips required
// to maximize a number with k set bits
using System;
  
class GFG {
      
    // function for finding set bit
    static int setBit(int xorValue)
    {
        int count = 0;
        while (xorValue >= 1) {
            if (xorValue % 2 == 1)
                count++;
      
            xorValue /= 2;
        }
          
        // return count of set bit
        return count;
    }
      
    // function for finding min flip
    static int minFlip(int n, int k)
    { 
        // number of bits in n
        int size = (int)(Math.Log(n) / 
                         Math.Log(2)) + 1;
          
        // Find the largest number of
        // same size with k set bits
        int max = (int)Math.Pow(2, k) - 1; 
        max = max << (size - k);
      
        // Count bit differences to find
        // required flipping.
        int xorValue = (n ^ max);
        return (setBit(xorValue));
    }
      
    // Driver Code
    public static void Main() 
    {
        int n = 27, k = 3;
        Console.Write("Min Flips = "+ minFlip(n, k));
    }
}
  
// This code is contributed by Nitin Mittal.


PHP


输出 :

Min Flips = 3