Find bitwise OR of all possible sub-arrays - GeeksforGeeks

11 May, 2021
Given an array A of size N where, . The task is to find the OR of all possible sub-arrays of A and then the OR of all these results.Examples:  
Input : 1 4 6
Output : 7
All possible subarrays are 
{1}, {1, 4}, {4, 6} and {1, 4, 6}
ORs of these subarrays are 1, 5, 6
and 7. OR of these ORs is 7.

Input : 10 100 1000
Output : 1006
 
Approach: In SET 1 we have seen how to find bitwise AND of all possible subarrays. A similar approach is also applicable here.The Naive solution is to find the OR of all the sub-arrays and then output the OR of their results. This will lead to O(N2) solution.Efficient Solution: Using the property that i:e it doesn’t matter how many times an element comes, it’s ORing will be counted as one only. Thus our problem boils down to finding the OR of all the elements of the array.Below is the implementation of above approach.  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find OR of all the sub-arrays#include <bits/stdc++.h>using namespace std; // function to return OR of sub-arraysint OR(int a[], int n){    int ans = a[0];    for (int i = 1; i < n; ++i)         ans |= a[i];        return ans;} // Driver programint main(){    int a[] = { 1, 4, 6 };    int n = sizeof(a) / sizeof(a[0]);     // print OR of all subarrays    cout << OR(a, n);     return 0;}
// Java program to find OR of// all the sub-arraysclass GFG{     // function to return OR// of sub-arraysstatic int OR(int a[],int n){    int ans = a[0];    int i;    for(i = 1; i < n; i++)    {        ans |= a[i];    }    return ans;} // Driver Codepublic static void main(String args[]){    int a[] = { 1, 4, 6};         int n = a.length;         // print OR of all subarrays    System.out.println(OR(a, n));}} // This code is contributed// by ANKITRAI1
# Python3 program to find OR of all the sub-arrays # function to return OR of sub-arraysdef OR(a, n):         ans = a[0]    for i in range(1,n):        ans |= a[i]         return ans     # Driver Codeif __name__=='__main__':    a = [1, 4, 6]    n = len(a) # print OR of all subarrays    print(OR(a, n)) # This code is contributed# by Shashank_Sharma
// C# program to find OR of// all the sub-arraysusing System; class GFG{     // function to return OR// of sub-arraysstatic int OR(int[] a, int n){    int ans = a[0];    int i;    for(i = 1; i < n; i++)    {        ans |= a[i];    }    return ans;} // Driver Codepublic static void Main(){    int[] a = { 1, 4, 6};         int n = a.Length;         // print OR of all subarrays    Console.Write(OR(a, n));}} // This code is contributed// by ChitraNayal
<?php// PHP program to find OR// of all the sub-arrays // function to return OR// of sub-arraysfunction O_R($a, $n){    $ans = $a[0];    for ($i = 1; $i < $n; ++$i)        $ans |= $a[$i];     return $ans;} // Driver Code$a = array( 1, 4, 6 );$n = count($a); // print OR of all subarraysecho O_R($a, $n); // This code is contributed// by inder_verma?>
<script> // Javascript program to find OR of all the sub-arrays // function to return OR of sub-arraysfunction OR(a, n){    var ans = a[0];    for (var i = 1; i < n; ++i)         ans |= a[i];        return ans;} var a = [ 1, 4, 6 ];var n = a.length;     // print OR of all subarrays document.write(OR(a, n)); // This code is contributed by SoumikMondal </script>
Output:  
7
Time Complexity: O(N) 
Shashank_Sharma
ankthon
ukasp
inderDuMCA
nidhi_biet
SoumikMondal
Bitwise-OR
Arrays
Bit Magic
Arrays
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Stack Data Structure (Introduction and Program)
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Introduction to Arrays
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)
Cyclic Redundancy Check and Modulo-2 Division
How to swap two numbers without using a temporary variable?