Sum of bitwise OR of all subarrays

22 Jun, 2021
Give 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. 1:- Traverse the from 0th position to n-1. 2:- 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
Java
Python3
C#
PHP
Javascript
// 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 codeint 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 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 codeint main(){    int a[] = { 1, 2, 3, 4, 5 };    int n = sizeof(a)/sizeof(a[0]);    printf("%d ", totalSum(a, n));    return 0;}
// 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 codepublic 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 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 codea = [1, 2, 3, 4, 5];n = len(a);print(totalSum(a, n)); # This code is contributed by mits
// 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 codestatic 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 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?>
<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>
71
 
tufan_gupta2000
Mithun Kumar
Shivi_Aggarwal
shivanisinghss2110
Bitwise-OR
Arrays
Bit Magic
Arrays
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Stack Data Structure (Introduction and Program)
Linear Search
Bitwise Operators in C/C++
Left Shift and Right Shift Operators in C/C++
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
How to swap two numbers without using a temporary variable?
Program to find whether a given number is power of 2