Find bitwise OR of all possible sub-arrays

Given an array A of size N where, 1\leq N \leq 10^{5}. 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. 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 X\|X\|...\|X=X 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find OR of all the sub-arrays
#include <bits/stdc++.h>
using namespace std;
  
// function to return OR of sub-arrays
int OR(int a[], int n)
{
    int ans = a[0];
    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[0]);
  
    // print OR of all subarrays
    cout << OR(a, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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[0];
    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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find OR of all the sub-arrays
  
# function to return OR of sub-arrays 
def OR(a, n):
      
    ans = a[0]
    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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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[0];
    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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find OR 
// of all the sub-arrays
  
// function to return OR
// of sub-arrays
function 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 subarrays
echo O_R($a, $n);
  
// This code is contributed
// by inder_verma
?>

chevron_right


Output:

7

Time Complexity: O(N)



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.