📌  相关文章
📜  从 0 到 N 的整数对的最大设置位计数,产生总和为 N

📅  最后修改于: 2021-10-27 08:46:18             🧑  作者: Mango

给定一个整数 N,任务是在从0 到 N 的所有整数对中找到设置位的最大频率,从而产生一个总和为 N。

例子:

朴素的方法:解决这个问题的最简单的方法是生成所有可能对的总和为N并计算所有这些对的设置位的最大和,并打印设置位总和的最大数目。

时间复杂度 O(N * log N)
辅助空间 O(1)

高效方法:上述方法可以通过以下步骤进行优化:

  • 找出一个小于等于N的数,其从最低有效位到最高有效位的所有位都是设置位。该数字将是该对中的第一个数字。
  • 计算对{first, N-first}的设置位数并将其相加。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the first number
int create_first_no(int n)
{
    // Length of the binary from
    int length = 0;
 
    // Number of set bits
    int freq_set_bits = 0;
    int ans = 0;
    while (n) {
 
        // Update the first number
        ans = ans << 1;
        ans = ans + 1;
 
        // Increment length
        length++;
 
        // Update the frequency
        if ((n & 1))
            freq_set_bits += 1;
 
        n = n >> 1;
    }
    // Check if n does not have all the
    // bits as set bits then make
    // the first as less than n
    if (length != freq_set_bits)
        ans = (ans >> 1);
 
    // Return the first value
    return ans;
}
 
// Function to calculate maximum
// set bit frequency sum
int maxSetBits(int n)
{
    // First value of pair
    int first = create_first_no(n);
 
    // Second value of pair
    int second = n - first;
 
    // __builtin_popcount() is inbuilt
    // function to count the number of set bits
    int freq_first
        = __builtin_popcount(first);
    int freq_second
        = __builtin_popcount(second);
 
    // Return the sum of freq of setbits
    return freq_first + freq_second;
}
 
// Driver Code
int main()
{
    int N = 5;
 
    // Function call
    cout << maxSetBits(N);
    return 0;
}


Java
// Java program to implement the
// above approach
import java.util.*;
 
class GFG {
 
// Function to find the first number
static int create_first_no(int n)
{
     
    // Length of the binary from
    int length = 0;
 
    // Number of set bits
    int freq_set_bits = 0;
    int ans = 0;
     
    while (n != 0)
    {
 
        // Update the first number
        ans = ans << 1;
        ans = ans + 1;
 
        // Increment length
        length++;
 
        // Update the frequency
        if ((n & 1) == 1)
            freq_set_bits += 1;
 
        n = n >> 1;
    }
     
    // Check if n does not have all the
    // bits as set bits then make
    // the first as less than n
    if (length != freq_set_bits)
        ans = (ans >> 1);
 
    // Return the first value
    return ans;
}
 
// Function to calculate maximum
// set bit frequency sum
static int maxSetBits(int n)
{
     
    // First value of pair
    int first = create_first_no(n);
 
    // Second value of pair
    int second = n - first;
 
    // Integer.bitCount() is inbuilt
    // function to count the number of set bits
    int freq_first = Integer.bitCount(first);
    int freq_second = Integer.bitCount(second);
 
    // Return the sum of freq of setbits
    return freq_first + freq_second;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 5;
     
    // Function call
    System.out.println(maxSetBits(N));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the
# above approach
 
# Function to find the
# first number
def create_first_no(n):
 
    # Length of the binary
    # from
    length = 0
 
    # Number of set bits
    freq_set_bits = 0
    ans = 0
    while (n != 0):
 
        # Update the first number
        ans = ans << 1
        ans = ans + 1
 
        # Increment length
        length += 1
 
        # Update the frequency
        if ((n & 1) != 0):
            freq_set_bits += 1
 
        n = n >> 1
 
    # Check if n does not have
    # all the bits as set bits
    # then make the first as
    # less than n
    if (length != freq_set_bits):
        ans = (ans >> 1)
 
    # Return the first value
    return ans
 
# Function to calculate maximum
# set bit frequency sum
def maxSetBits(n):
 
    # First value of pair
    first = create_first_no(n)
 
    # Second value of pair
    second = n - first
 
    # __builtin_popcount() is
    # inbuilt function to count
    # the number of set bits
    freq_first = bin(first).count('1')
    freq_second = bin(second).count('1')
 
    # Return the sum of
    # freq of setbits
    return (freq_first +
            freq_second)
 
# Driver code
N = 5
 
# Function call
print(maxSetBits(N))
 
# This code is contributed by divyeshrabadiya07


C#
// C# program to implement the
// above approach
using System;
using System.Linq;
class GFG {
  
// Function to find the first number
static int create_first_no(int n)
{
      
    // Length of the binary from
    int length = 0;
  
    // Number of set bits
    int freq_set_bits = 0;
    int ans = 0;
      
    while (n != 0)
    {
  
        // Update the first number
        ans = ans << 1;
        ans = ans + 1;
  
        // Increment length
        length++;
  
        // Update the frequency
        if ((n & 1) == 1)
            freq_set_bits += 1;
  
        n = n >> 1;
    }
      
    // Check if n does not have all the
    // bits as set bits then make
    // the first as less than n
    if (length != freq_set_bits)
        ans = (ans >> 1);
  
    // Return the first value
    return ans;
}
public static int countSetBits(int n)
{
 
  // base case
  if (n == 0)
    return 0;
 
  else
 
    // if last bit set
    // add 1 else add 0
    return (n & 1) + countSetBits(n >> 1);
}
 
// Function to calculate maximum
// set bit frequency sum
static int maxSetBits(int n)
{
      
    // First value of pair
    int first = create_first_no(n);
  
    // Second value of pair
    int second = n - first;
  
    //countSetBits function to
    //count the number of set bits
    int freq_first = countSetBits(first);
    int freq_second = countSetBits(second);
  
    // Return the sum of freq of setbits
    return freq_first + freq_second;
}
  
// Driver code
public static void Main(string[] args)
{
    int N = 5;
      
    // Function call
    Console.Write(maxSetBits(N));
}
}
  
// This code is contributed by Ritik Bansal


Javascript


输出:
3

时间复杂度: O(logN)
辅助空间: O(1)