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++
// 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
// 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
# 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#
// 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// 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?> |
Javascript
<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)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live


