# Find bitwise OR of all possible sub-arrays

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


## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: In SET 1 we have seen how to find bitwise AND of all possible subarrays. Similar approach is also aplicable 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  using namespace std;     // function to return OR of sub-arrays  int OR(int a[], int n)  {      int ans = a;      for (int i = 1; i < n; ++i)            ans |= a[i];             return ans;  }     // Driver program  int main()  {      int a[] = { 1, 4, 6 };      int n = sizeof(a) / sizeof(a);         // print OR of all subarrays      cout << OR(a, n);         return 0;  }

## Java

 // Java program to find OR of  // all the sub-arrays   class GFG   {         // function to return OR   // of sub-arrays   static int OR(int a[],int n)  {      int ans = a;      int i;      for(i = 1; i < n; i++)      {          ans |= a[i];      }      return ans;  }     // Driver Code   public 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-arrays   def OR(a, n):             ans = a      for i in range(1,n):          ans |= a[i]             return ans         # Driver Code  if __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-arrays   using System;     class GFG   {         // function to return OR   // of sub-arrays   static int OR(int[] a, int n)  {      int ans = a;      int i;      for(i = 1; i < n; i++)      {          ans |= a[i];      }      return ans;  }     // Driver Code   public 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

 

Output:

7

Time Complexity: O(N)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.