📌  相关文章
📜  数组中的最大设置位总和,不考虑相邻元素

📅  最后修改于: 2021-05-07 08:15:11             🧑  作者: Mango

给定整数数组arr []。任务是找到(数组元素的)设置位的最大和,而不将数组相邻元素的设置位相加。

例子:

Input : arr[] = {1, 2, 4, 5, 6, 7, 20, 25}
Output : 9

Input : arr[] = {5, 7, 9, 5, 13, 7, 20, 25}
Output : 11

方法

  1. 首先,找到数组中每个元素的置位总数,并将它们存储在不同的数组或同一数组中(以避免使用多余的空间)。
  2. 现在,问题减少了,可以在数组中找到最大和,以使没有两个元素相邻。
  3. 循环访问arr []中的所有元素,并维护两个和incl和excl,其中incl =包括前一个元素的最大和,而excl =除去前一个元素的最大和。
  4. 排除当前元素的最大和为max(incl,excl),包括当前元素的最大和为excl + current元素(请注意,仅考虑excl是因为元素不能相邻)。
  5. 在循环结束时,返回最大值incl和excl。

下面是上述方法的实现:

C++
// C++ program to maximum set bit sum in array
// without considering adjacent elements
#include
using namespace std;
  
// Function to count total number 
// of set bits in an integer
int bit(int n)
{
    int count = 0;
      
    while(n)
    {
        count++;
        n = n & (n - 1);
    }
      
    return count;
}
  
// Maximum sum of set bits
int maxSumOfBits(int arr[], int n)
{ 
    // Calculate total number of 
    // set bits for every element 
    // of the array
    for(int i = 0; i < n; i++)
    {
        // find total set bits for
        // each number and store 
        // back into the array
        arr[i] = bit(arr[i]);
    }
      
    int incl = arr[0]; 
    int excl = 0; 
    int excl_new; 
      
    for (int i = 1; i < n; i++) 
    { 
        // current max excluding i 
        excl_new = (incl > excl) ? 
                            incl : excl; 
  
        // current max including i 
        incl = excl + arr[i]; 
        excl = excl_new; 
    } 
  
    // return max of incl and excl 
    return ((incl > excl) ?
                     incl : excl); 
}
  
// Driver code
int main()
{
    int arr[] = {1, 2, 4, 5, 
                 6, 7, 20, 25};
      
    int n = sizeof(arr) / sizeof(arr[0]);
      
    cout << maxSumOfBits(arr, n);
      
    return 0;
}


Java
// Java program to maximum set bit sum in array
// without considering adjacent elements
import java.util.*;
import java.lang.*;
import java.io.*;
  
class GFG
{
// Function to count total number  
// of set bits in an integer
static int bit(int n)
{
    int count = 0;
      
    while(n > 0)
    {
        count++;
        n = n & (n - 1);
    }
      
    return count;
}
  
// Maximum sum of set bits
static int maxSumOfBits(int arr[], int n)
{ 
// Calculate total number of set bits
// for every element of the array
for(int i = 0; i < n; i++)
{
    // find total set bits for 
    // each number and store 
    // back into the array
    arr[i] = bit(arr[i]);
}
  
int incl = arr[0]; 
int excl = 0; 
int excl_new; 
  
for (int i = 1; i < n; i++) 
{ 
    // current max excluding i 
    excl_new = (incl > excl) ?  
                        incl : excl; 
  
    // current max including i 
    incl = excl + arr[i]; 
    excl = excl_new; 
} 
  
// return max of incl and excl 
return ((incl > excl) ? 
                 incl : excl); 
}
  
// Driver code
public static void main(String args[])
{
    int arr[] = {1, 2, 4, 5, 
                 6, 7, 20, 25};
      
    int n = arr.length;
      
    System.out.print(maxSumOfBits(arr, n));
} 
}
  
// This code is contributed
// by Subhadeep


Python3
# Python3 program to maximum set bit sum in 
# array without considering adjacent elements
  
# Function to count total number 
# of set bits in an integer
def bit(n):
  
    count = 0
      
    while(n):
      
        count += 1
        n = n & (n - 1)
      
    return count
  
# Maximum sum of set bits
def maxSumOfBits(arr, n):
  
    # Calculate total number of set bits
    # for every element of the array
    for i in range( n):
      
        # find total set bits for each 
        # number and store back into the array
        arr[i] = bit(arr[i])
      
    incl = arr[0]
    excl = 0
      
    for i in range(1, n) :
      
        # current max excluding i 
        if incl > excl:
            excl_new = incl 
        else:
            excl_new = excl
  
        # current max including i 
        incl = excl + arr[i]; 
        excl = excl_new
      
    # return max of incl and excl 
    if incl > excl:
        return incl 
    else :
        return excl
  
# Driver code
if __name__ == "__main__":
  
    arr = [1, 2, 4, 5, 
           6, 7, 20, 25]
      
    n = len(arr)
      
    print (maxSumOfBits(arr, n))
      
# This code is contributed by ita_c


C#
// C# program to maximum set bit sum in array
// without considering adjacent elements
using System;
  
class GFG
{
// Function to count total number 
// of set bits in an integer
static int bit(int n)
{
    int count = 0;
      
    while(n > 0)
    {
        count++;
        n = n & (n - 1);
    }
      
    return count;
}
  
// Maximum sum of set bits
static int maxSumOfBits(int []arr, int n)
{ 
      
// Calculate total number of set bits
// for every element of the array
for(int i = 0; i < n; i++)
{
      
    // find total set bits for 
    // each number and store 
    // back into the array
    arr[i] = bit(arr[i]);
}
  
int incl = arr[0]; 
int excl = 0; 
int excl_new; 
  
for (int i = 1; i < n; i++) 
{ 
    // current max excluding i 
    excl_new = (incl > excl) ? 
                        incl : excl; 
  
    // current max including i 
    incl = excl + arr[i]; 
    excl = excl_new; 
} 
  
// return max of incl and excl 
return ((incl > excl) ? 
                 incl : excl); 
}
  
// Driver code
public static void Main()
{
    int []arr = {1, 2, 4, 5, 
                 6, 7, 20, 25};
      
    int n = arr.Length;
      
    Console.WriteLine(maxSumOfBits(arr, n));
} 
}
  
// This code is contributed
// by chandan_jnu.


PHP
 $excl) ? 
                            $incl : $excl; 
  
        // current max including i 
        $incl = $excl + $arr[$i]; 
        $excl = $excl_new; 
    } 
  
    // return max of incl and excl 
    return (($incl > $excl) ? 
                    $incl : $excl); 
} 
  
// Driver code 
  
    $arr = array(1, 2, 4, 5, 
                6, 7, 20, 25); 
      
     $n = sizeof($arr) / sizeof($arr[0]); 
      
    echo  maxSumOfBits($arr, $n); 
      
  
#This Code is Contributed by ajit    
?>


输出:

9

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

注意:可以使用__builtin_popcount函数将上述代码优化为O(N),以对O(1)时间中的设置位进行计数。