Open In App

Bitwise OR of sum of all subsequences of an array

Given an array arr[] of length N, the task is to find the Bitwise OR of the sum of all possible subsequences from the given array.

Examples:



Input: arr[] = {4, 2, 5}
Output: 15
Explanation: All subsequences from the given array and their corresponding sums:
{4} – 4
{2} – 2
{5} – 5
{4, 2} – 6
{4, 5} – 9
{2, 5} – 7
{4, 2, 5} -11
Therefore, the Bitwise OR of all sums = 4 | 2 | 5 | 6 | 9 | 7 | 11 = 15.

Input: arr[] = {1, 9, 8}
Output: 27
Explanation: All subsequences from the given array and their corresponding sums:
{1} – 1
{9} – 9
{8} – 8
{1, 9} – 10
{9, 8} – 17
{1, 8} – 9
{1, 9, 8} – 18
Therefore, Bitwise OR of all sums = 1 | 9 | 8 | 10 | 17 | 9 | 18 = 27.



Naive Approach: The simplest approach is to generate all possible subsequences from the given array and find their respective sums. Now, after calculating their sums, print the Bitwise OR of all the sums obtained. 

Time Complexity: O(2N)
Auxiliary Space: O(1)

Efficient approach: To optimize the above approach, the idea is based on the following observations:

Follow the steps below to solve the above problem:

Below is the implementation of the above approach:




// C++ program for the above approach
#include <bits/stdc++.h>
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 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 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# 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




<script>
 
// JavaScript program for the above approach
 
// Function to calculate Bitwise OR of
// sums of all subsequences
function findOR(nums, N)
{
    // Stores the prefix sum of nums[]
    let prefix_sum = 0;
  
    // Stores the bitwise OR of
    // sum of each subsequence
    let result = 0;
  
    // Iterate through array nums[]
    for (let 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
 
   // Given array arr[]
    let arr = [ 4, 2, 5 ];
    let N = arr.length;
    document.write(findOR(arr, N));
   
  // This code is contributed by avijitmondal1998.
</script>

Output: 
15

 

Time Complexity: O(N)
Auxiliary Space: O(1)


Article Tags :