Sum of bitwise OR of all subarrays
Last Updated :
28 Feb, 2023
Given an array of positive integers, find the total sum after performing the bit wise OR operation on all the sub arrays of a given array.
Examples:
Input : 1 2 3 4 5
Output : 71
Input : 6 5 4 3 2
Output : 84

First initialize the two variable sum=0, sum1=0, variable sum will store the total sum and, with sum1 we will perform bitwise OR operation for each jth element, and add sum1 with sum.
- Traverse the from 0th position to n-1.
- For each ith variable we will perform bit wise OR operation on all the sub arrays to find the total sum.
Repeat step until the whole array is traverse.
C++
// C++ program to find sum of
// bitwise ors of all subarrays.
#include <iostream>
using namespace std;
int totalSum(int a[], int n)
{
int i, sum = 0, sum1 = 0, j;
for (i = 0; i < n; i++)
{
sum1 = 0;
// perform Bitwise OR operation
// on all the subarray present
// in array
for (j = i; j < n; j++)
{
// OR operation
sum1 = (sum1 | a[j]);
// now add the sum after performing
// the Bitwise OR operation
sum = sum + sum1;
}
}
return sum;
}
// Driver code
int main()
{
int a[] = { 1, 2, 3, 4, 5 };
int n = sizeof(a) / sizeof(a[0]);
cout << totalSum(a, n) << endl;
return 0;
}
// This code is contributed
// by Shivi_Aggarwal
C
// C program to find sum of bitwise ors
// of all subarrays.
#include <stdio.h>
int totalSum(int a[], int n)
{
int i, sum = 0, sum1 = 0, j;
for (i = 0; i < n; i++) {
sum1 = 0;
// perform Bitwise OR operation
// on all the subarray present in array
for (j = i; j < n; j++) {
// OR operation
sum1 = (sum1 | a[j]);
// now add the sum after performing the
// Bitwise OR operation
sum = sum + sum1;
}
}
return sum;
}
// Driver code
int main()
{
int a[] = { 1, 2, 3, 4, 5 };
int n = sizeof(a)/sizeof(a[0]);
printf("%d ", totalSum(a, n));
return 0;
}
Java
// Java program to find sum
// of bitwise ors of all subarrays.
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
static int totalSum(int a[], int n)
{
int i, sum = 0, sum1 = 0, j;
for (i = 0; i < n; i++)
{
sum1 = 0;
// perform Bitwise OR operation
// on all the subarray present
// in array
for (j = i; j < n; j++)
{
// OR operation
sum1 = (sum1 | a[j]);
// now add the sum after
// performing the Bitwise
// OR operation
sum = sum + sum1;
}
}
return sum;
}
// Driver code
public static void main(String args[])
{
int a[] = { 1, 2, 3, 4, 5 };
int n = a.length;
System.out.println(totalSum(a,n));
}
}
// This code is contributed
// by Subhadeep
Python3
# Python3 program to find sum of
# bitwise ors of all subarrays.
def totalSum(a, n):
sum = 0;
for i in range(n):
sum1 = 0;
# perform Bitwise OR operation
# on all the subarray present
# in array
for j in range(i, n):
# OR operation
sum1 = (sum1 | a[j]);
# now add the sum after
# performing the
# Bitwise OR operation
sum = sum + sum1;
return sum;
# Driver code
a = [1, 2, 3, 4, 5];
n = len(a);
print(totalSum(a, n));
# This code is contributed by mits
C#
// C# program to find sum
// of bitwise ors of all
// subarrays.
using System;
class GFG
{
static int totalSum(int[] a, int n)
{
int sum = 0;
for(int i = 0; i < n; i++)
{
int sum1 = 0;
// perform Bitwise OR operation
// on all the subarray present
// in array
for (int j = i; j < n; j++)
{
// OR operation
sum1 = (sum1 | a[j]);
// now add the sum after
// performing the Bitwise
// OR operation
sum = sum + sum1;
}
}
return sum;
}
// Driver code
static void Main()
{
int[] a = { 1, 2, 3, 4, 5 };
int n = a.Length;
Console.WriteLine(totalSum(a,n));
}
}
// This code is contributed
// by mits
PHP
<?php
// PHP program to find
// sum of bitwise ors
// of all subarrays.
function totalSum($a,$n)
{
$sum = 0;
for ($i = 0; $i < $n; $i++)
{
$sum1 = 0;
// perform Bitwise OR operation
// on all the subarray present
// in array
for ($j = $i; $j < $n; $j++)
{
// OR operation
$sum1 = ($sum1 | $a[$j]);
// now add the sum after
// performing the
// Bitwise OR operation
$sum = $sum + $sum1;
}
}
return $sum;
}
// Driver code
$a = array(1, 2, 3, 4, 5);
$n = sizeof($a);
echo totalSum($a, $n);
// This code is contributed by mits
?>
JavaScript
<script>
// Java program to find sum
// of bitwise ors of all subarrays.
function totalSum(a, n)
{
let i, sum = 0, sum1 = 0, j;
for (i = 0; i < n; i++)
{
sum1 = 0;
// perform Bitwise OR operation
// on all the subarray present
// in array
for (j = i; j < n; j++)
{
// OR operation
sum1 = (sum1 | a[j]);
// now add the sum after
// performing the Bitwise
// OR operation
sum = sum + sum1;
}
}
return sum;
}
// Driver code
let a = [ 1, 2, 3, 4, 5 ];
let n = a.length;
document.write(totalSum(a,n));
// This code is contributed shivanisinghss2110
</script>
Time Complexity: O(N*N)
Auxiliary Space: O(1)
Similar Reads
Sum of bitwise AND of all subarrays Given an array consisting of N positive integers, find the sum of bit-wise and of all possible sub-arrays of the array. Examples: Input : arr[] = {1, 5, 8} Output : 15 Bit-wise AND of {1} = 1 Bit-wise AND of {1, 5} = 1 Bit-wise AND of {1, 5, 8} = 0 Bit-wise AND of {5} = 5 Bit-wise AND of {5, 8} = 0
8 min read
Sum of Bitwise-OR of all Submatrices Given a NxN matrix, the task is to find the sum of bit-wise OR of all of its rectangular sub-matrices.Examples: Input : arr[][] = {{1, 0, 0}, {0, 0, 0}, {0, 0, 0}} Output : 9 Explanation: All the submatrices starting from the index (0, 0) will have OR value as 1. Thus, ans = 9 Input : arr[][] = {{9,
14 min read
Sum of bitwise AND of all submatrices Given an NxN matrix, the task is to find the sum of bit-wise AND of all of its rectangular sub-matrices. Examples: Input : arr[][] = {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}} Output : 36 Explanation: All the possible submatrices will have AND value 1. Since, there are 36 submatrices in total, ans = 36 Input
13 min read
Count distinct Bitwise OR of all Subarrays Given an array A of non-negative integers, where 0 \leq A[i] \leq 10^{9} . The task is to count number of distinct possible results obtained by taking the bitwise OR of all the elements in all possible Subarrays. Examples: Input: A = [1, 2] Output: 3 Explanation: The possible subarrays are [1], [2],
7 min read
Queries for Sum of Bitwise AND of all Subarrays in a Range Given an array arr[] of size N, the task is to answer a set of Q queries, each in the format of queries[i][0] and queries[i][1]. For each queries[i], find the sum of Bitwise AND of all subarrays whose elements lie in the range [queries[i][0], queries[i][1]]. Examples: Input: N = 3, arr[] = {1, 0, 2}
12 min read