Bitwise OR of Bitwise AND of all subarrays of an array
Last Updated :
20 Oct, 2021
Given an array arr[] consisting of N positive integers, the task is to find the Bitwise OR of Bitwise AND of all subarrays of the given arrays.
Examples:
Input: arr[] = {1, 2, 3}
Output: 3
Explanation:
The following are Bitwise AND of all possible subarrays are:
- {1}, Bitwise AND is 1.
- {1, 2}, Bitwise AND is 0.
- {1, 2, 3}, Bitwise AND is 0.
- {2}, Bitwise AND is 2.
- {2, 3}, Bitwise AND is 2.
- {3}, Bitwise AND is 3.
The Bitwise OR of all the above values is 3.
Input: arr[] = {1, 4, 2, 10}
Output: 15
Naive Approach: The simplest approach to solve the given problem is to generate all possible subarray of the given array and then find the Bitwise OR of all Bitwise AND of all the generated subarray as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findbitwiseOR( int * a, int n)
{
int res = 0;
for ( int i = 0; i < n; i++) {
int curr_sub_array = a[i];
res = res | curr_sub_array;
for ( int j = i; j < n; j++) {
curr_sub_array = curr_sub_array
& a[j];
res = res | curr_sub_array;
}
}
cout << res;
}
int main()
{
int A[] = { 1, 2, 3 };
int N = sizeof (A) / sizeof (A[0]);
findbitwiseOR(A, N);
return 0;
}
|
Java
public class GFG {
static void findbitwiseOR( int [] a, int n)
{
int res = 0 ;
for ( int i = 0 ; i < n; i++) {
int curr_sub_array = a[i];
res = res | curr_sub_array;
for ( int j = i; j < n; j++) {
curr_sub_array = curr_sub_array & a[j];
res = res | curr_sub_array;
}
}
System.out.println(res);
}
public static void main(String[] args)
{
int A[] = { 1 , 2 , 3 };
int N = A.length;
findbitwiseOR(A, N);
}
}
|
Python3
def findbitwiseOR(a, n):
res = 0
for i in range (n):
curr_sub_array = a[i]
res = res | curr_sub_array
for j in range (i, n):
curr_sub_array = curr_sub_array & a[j]
res = res | curr_sub_array
print (res)
if __name__ = = '__main__' :
A = [ 1 , 2 , 3 ]
N = len (A)
findbitwiseOR(A, N)
|
C#
using System;
class GFG{
static void findbitwiseOR( int [] a, int n)
{
int res = 0;
for ( int i = 0; i < n; i++) {
int curr_sub_array = a[i];
res = res | curr_sub_array;
for ( int j = i; j < n; j++) {
curr_sub_array = curr_sub_array & a[j];
res = res | curr_sub_array;
}
}
Console.Write(res);
}
static void Main()
{
int [] A = { 1, 2, 3 };
int N = A.Length;
findbitwiseOR(A, N);
}
}
|
Javascript
<script>
function findbitwiseOR(a, n)
{
let res = 0;
for (let i = 0; i < n; i++) {
let curr_sub_array = a[i];
res = res | curr_sub_array;
for (let j = i; j < n; j++) {
curr_sub_array = curr_sub_array & a[j];
res = res | curr_sub_array;
}
}
document.write(res);
}
let A = [ 1, 2, 3 ];
let N = A.length;
findbitwiseOR(A, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized based on the observation that the Bitwise AND of any subarray is always less than or equal to the first element in the subarray. Therefore, the maximum possible value is the Bitwise AND of the subarrays are the elements themselves. Therefore, the task is reduced to finding the Bitwise OR of all the array elements as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findbitwiseOR( int * a, int n)
{
int res = 0;
for ( int i = 0; i < n; i++)
res = res | a[i];
cout << res;
}
int main()
{
int A[] = { 1, 2, 3 };
int N = sizeof (A) / sizeof (A[0]);
findbitwiseOR(A, N);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void findbitwiseOR( int [] a, int n)
{
int res = 0 ;
for ( int i = 0 ; i < n; i++)
res = res | a[i];
System.out.println(res);
}
public static void main(String[] args)
{
int [] A = { 1 , 2 , 3 };
int N = A.length;
findbitwiseOR(A, N);
}
}
|
Python3
def findbitwiseOR(a, n):
res = 0
for i in range (n):
res = res | a[i]
print (res)
if __name__ = = '__main__' :
A = [ 1 , 2 , 3 ]
N = len (A)
findbitwiseOR(A, N)
|
C#
using System;
class GFG{
static void findbitwiseOR( int [] a, int n)
{
int res = 0;
for ( int i = 0; i < n; i++)
res = res | a[i];
Console.Write(res);
}
public static void Main()
{
int [] A = { 1, 2, 3 };
int N = A.Length;
findbitwiseOR(A, N);
}
}
|
Javascript
<script>
function findbitwiseOR(a, n)
{
var res = 0;
var i;
for (i = 0; i < n; i++)
res = res | a[i];
document.write(res);
}
var A = [1, 2, 3];
var N = A.length;
findbitwiseOR(A, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...