📜  数组所有子序列之和的按位或

📅  最后修改于: 2021-05-25 05:20:27             🧑  作者: Mango

给定长度为N的数组arr [] ,任务是从给定数组中找到所有可能的子序列之和的按位或。

例子:

天真的方法:最简单的方法是从给定数组生成所有可能的子序列,并找到它们各自的和。现在,在计算了它们的总和之后,打印所有获得的总和的按位或。

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

高效的方法:为了优化上述方法,该想法是基于以下观察结果:

  • 数组元素中的所有设置位也都设置在最终结果中。
  • 给定数组的前缀和数组中设置的所有位也都设置在最终结果中。

请按照以下步骤解决以上问题:

  • 0初始化一个变量结果,该结果存储给定数组arr []的每个子序列之和的按位或
  • 0初始化一个变量prefixSum ,该变量在任何时刻存储arr []的前缀和。
  • 使用变量i遍历[0,N]范围内的数组元素。
    • 更新prefixSum作为prefixSum + = ARR [i]中
    • 结果更新结果| = arr [i] | prefixSum。
  • 完成上述步骤后,将结果值打印为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to calculate Bitwise OR of
// sums of all subsequences
int findOR(int nums[], int N)
{
    // Stores the prefix sum of nums[]
    int prefix_sum = 0;
 
    // Stores the bitwise OR of
    // sum of each subsequence
    int result = 0;
 
    // Iterate through array nums[]
    for (int i = 0; i < N; i++) {
 
        // Bits set in nums[i] are
        // also set in result
        result |= nums[i];
 
        // Calculate prefix_sum
        prefix_sum += nums[i];
 
        // Bits set in prefix_sum
        // are also set in result
        result |= prefix_sum;
    }
 
    // Return the result
    return result;
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 4, 2, 5 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << findOR(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
   
// Function to calculate Bitwise OR of
// sums of all subsequences
static int findOR(int nums[], int N)
{
    // Stores the prefix sum of nums[]
    int prefix_sum = 0;
 
    // Stores the bitwise OR of
    // sum of each subsequence
    int result = 0;
 
    // Iterate through array nums[]
    for (int i = 0; i < N; i++) {
 
        // Bits set in nums[i] are
        // also set in result
        result |= nums[i];
 
        // Calculate prefix_sum
        prefix_sum += nums[i];
 
        // Bits set in prefix_sum
        // are also set in result
        result |= prefix_sum;
    }
 
    // Return the result
    return result;
}
 
// Driver Code
public static void main(String[] args)
{
    // Given array arr[]
    int arr[] = { 4, 2, 5 };
    int N = arr.length;
    System.out.print(findOR(arr, N));
}
}


Python3
# Python3 program for the
# above approach
 
# Function to calculate
# Bitwise OR of sums of
# all subsequences
def findOR(nums,  N):
 
    # Stores the prefix
    # sum of nums[]
    prefix_sum = 0
 
    # Stores the bitwise OR of
    # sum of each subsequence
    result = 0
 
    # Iterate through array nums[]
    for i in range(N):
 
        # Bits set in nums[i] are
        # also set in result
        result |= nums[i]
 
        # Calculate prefix_sum
        prefix_sum += nums[i]
 
        # Bits set in prefix_sum
        # are also set in result
        result |= prefix_sum
 
    # Return the result
    return result
 
# Driver Code
if __name__ == "__main__":
 
    # Given array arr[]
    arr = [4, 2, 5]
 
    N = len(arr)
 
    # Function Call
    print(findOR(arr, N))
 
# This code is contributed by Chitranayal


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to calculate Bitwise OR of
// sums of all subsequences
static int findOR(int[] nums, int N)
{
     
    // Stores the prefix sum of nums[]
    int prefix_sum = 0;
 
    // Stores the bitwise OR of
    // sum of each subsequence
    int result = 0;
 
    // Iterate through array nums[]
    for(int i = 0; i < N; i++)
    {
         
        // Bits set in nums[i] are
        // also set in result
        result |= nums[i];
 
        // Calculate prefix_sum
        prefix_sum += nums[i];
 
        // Bits set in prefix_sum
        // are also set in result
        result |= prefix_sum;
    }
 
    // Return the result
    return result;
}
 
// Driver Code
public static void Main()
{
     
    // Given array arr[]
    int[] arr = { 4, 2, 5 };
     
    // Size of array
    int N = arr.Length;
     
    // Function call
    Console.Write(findOR(arr, N));
}
}
 
// This code is contributed by code_hunt


Javascript


输出:
15

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