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

📅  最后修改于: 2021-09-04 11:42:55             🧑  作者: Mango

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

例子:

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

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

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

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

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

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

下面是上述方法的实现:

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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live