📌  相关文章
📜  按位与非零的子序列的最大和

📅  最后修改于: 2022-05-13 01:56:07.916000             🧑  作者: Mango

按位与非零的子序列的最大和

给定一个由N个整数组成的数组arr[] ,任务是从数组中找到其元素的按位与不等于 0 的任何子序列的最大和。

例子:

朴素方法:解决给定问题的最简单方法是生成给定数组的所有可能子序列,并打印该子序列的所有非零元素的按位与的最大和。

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

有效方法:上述方法也可以通过观察以下事实来优化:仅在所有所选数组元素中设置了位的那些元素的总和给出了位与非零的子序列。因此,我们的想法是最大化所有这些元素的总和。请按照以下步骤解决问题:

  • 初始化一个变量,比如说ans ,它存储具有按位与值的子序列的最大总和为正数。
  • 使用变量i遍历范围[0, 32]并执行以下步骤:
    • 初始化一个变量,比如sum ,它存储所有第i被设置的元素的总和。
    • 遍历给定的数组,如果数组元素arr[i]第 i被设置,则将此值添加到变量sum中。
    • ans 的值更新为 anssum的最大值。
  • 完成上述步骤后,打印总和的值作为子序列的最大总和。

下面是我们方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum sum of
// a subsequence whose Bitwise AND is non-zero
int maximumSum(int arr[], int N)
{
    // Stores the resultant maximum
    // sum of the subsequence
    int ans = 0;
 
    // Iterate over all the bits
    for (int bit = 0; bit < 32; bit++) {
 
        // Stores the sum of array
        // elements whose i-th bit is set
        int sum = 0;
 
        // Traverse the array elements
        for (int i = 0; i < N; i++) {
 
            // If the bit is set, then
            // add its value to the sum
            if (arr[i] & (1 << bit)) {
                sum += arr[i];
            }
        }
 
        // Update the resultant
        // maximum sum
        ans = max(ans, sum);
    }
 
    // Return the maximum sum
    return ans;
}
 
// Driver Code
int main()
{
    int arr[] = { 5, 4, 1, 7, 11 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << maximumSum(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
public class GFG
{
   
 // Function to find the maximum sum of
 // a subsequence whose Bitwise AND is non-zero
 static int maximumSum(int arr[], int N)
 {
    
     // Stores the resultant maximum
     // sum of the subsequence
     int ans = 0;
 
     // Iterate over all the bits
     for (int bit = 0; bit < 32; bit++) {
 
         // Stores the sum of array
         // elements whose i-th bit is set
         int sum = 0;
 
         // Traverse the array elements
         for (int i = 0; i < N; i++) {
 
             // If the bit is set, then
             // add its value to the sum
             if ((arr[i] & (1 << bit)) == 1) {
                 sum += arr[i];
             }
         }
 
         // Update the resultant
         // maximum sum
         ans = Math.max(ans, sum);
     }
 
     // Return the maximum sum
     return ans;
 }
 
    // Driver code
    public static void main(String[] args)
    {   
        int arr[] = { 5, 4, 1, 7, 11 };
        int N = arr.length;
       System.out.println(maximumSum(arr, N));
    }
}
 
// This code is contributed by abhinavjain194


Python3
# python3 program for the above approach
 
# Function to find the maximum sum of
# a subsequence whose Bitwise AND is non-zero
def maximumSum(arr, N):
   
    # Stores the resultant maximum
    # sum of the subsequence
    ans = 0
 
    # Iterate over all the bits
    for bit in range(32):
       
        # Stores the sum of array
        # elements whose i-th bit is set
        sum = 0
 
        # Traverse the array elements
        for i in range(N):
           
            # If the bit is set, then
            # add its value to the sum
            if (arr[i] & (1 << bit)):
                sum += arr[i]
 
        # Update the resultant
        # maximum sum
        ans = max(ans, sum)
 
    # Return the maximum sum
    return ans
 
# Driver Code
if __name__ == '__main__':
    arr = [5, 4, 1, 7, 11]
    N = len(arr)
    print(maximumSum(arr, N))
 
    # This code is contributed by bgangwar59.


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the maximum sum of
// a subsequence whose Bitwise AND is non-zero
static int maximumSum(int[] arr, int N)
{
     
    // Stores the resultant maximum
    // sum of the subsequence
    int ans = 0;
 
    // Iterate over all the bits
    for(int bit = 0; bit < 32; bit++)
    {
         
        // Stores the sum of array
        // elements whose i-th bit is set
        int sum = 0;
 
        // Traverse the array elements
        for(int i = 0; i < N; i++)
        {
             
            // If the bit is set, then
            // add its value to the sum
            if ((arr[i] & (1 << bit)) != 0)
            {
                sum += arr[i];
            }
        }
 
        // Update the resultant
        // maximum sum
        ans = Math.Max(ans, sum);
    }
 
    // Return the maximum sum
    return ans;
}
 
// Driver code
static public void Main()
{
    int[] arr = { 5, 4, 1, 7, 11 };
    int N = arr.Length;
     
    Console.Write(maximumSum(arr, N));
}
}
 
// This code is contributed by offbeat


Javascript


输出:
24

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