Bitwise OR of sum of all subsequences of an array
Last Updated :
23 Apr, 2021
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:
- All the set bits in the array elements are also set in the final result.
- All the bits set in the prefix sum array of the given array are also set in the final result.
Follow the steps below to solve the above problem:
- Initialize a variable result with 0 that stores the Bitwise OR of the sum of each subsequence of the given array arr[].
- Initialize a variable prefixSum with 0 that stores the prefix sum of arr[] at any instant.
- Iterate over the array elements in the range [0, N] using variable i.
- Update prefixSumas prefixSum+= arr[i].
- Update result as result | = arr[i] | prefixSum.
- After the above steps, print the value of the result as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findOR( int nums[], int N)
{
int prefix_sum = 0;
int result = 0;
for ( int i = 0; i < N; i++) {
result |= nums[i];
prefix_sum += nums[i];
result |= prefix_sum;
}
return result;
}
int main()
{
int arr[] = { 4, 2, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << findOR(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int findOR( int nums[], int N)
{
int prefix_sum = 0 ;
int result = 0 ;
for ( int i = 0 ; i < N; i++) {
result |= nums[i];
prefix_sum += nums[i];
result |= prefix_sum;
}
return result;
}
public static void main(String[] args)
{
int arr[] = { 4 , 2 , 5 };
int N = arr.length;
System.out.print(findOR(arr, N));
}
}
|
Python3
def findOR(nums, N):
prefix_sum = 0
result = 0
for i in range (N):
result | = nums[i]
prefix_sum + = nums[i]
result | = prefix_sum
return result
if __name__ = = "__main__" :
arr = [ 4 , 2 , 5 ]
N = len (arr)
print (findOR(arr, N))
|
C#
using System;
class GFG{
static int findOR( int [] nums, int N)
{
int prefix_sum = 0;
int result = 0;
for ( int i = 0; i < N; i++)
{
result |= nums[i];
prefix_sum += nums[i];
result |= prefix_sum;
}
return result;
}
public static void Main()
{
int [] arr = { 4, 2, 5 };
int N = arr.Length;
Console.Write(findOR(arr, N));
}
}
|
Javascript
<script>
function findOR(nums, N)
{
let prefix_sum = 0;
let result = 0;
for (let i = 0; i < N; i++) {
result |= nums[i];
prefix_sum += nums[i];
result |= prefix_sum;
}
return result;
}
let arr = [ 4, 2, 5 ];
let N = arr.length;
document.write(findOR(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...