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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the Bitwise OR
// of Bitwise AND of all subarrays
void findbitwiseOR(int* a, int n)
{
// Stores the required result
int res = 0;
// Generate all the subarrays
for (int i = 0; i < n; i++) {
// Store the current element
int curr_sub_array = a[i];
// Find the Bitwise OR
res = res | curr_sub_array;
for (int j = i; j < n; j++) {
// Update the result
curr_sub_array = curr_sub_array
& a[j];
res = res | curr_sub_array;
}
}
// Print the result
cout << res;
}
// Driver Code
int main()
{
int A[] = { 1, 2, 3 };
int N = sizeof(A) / sizeof(A[0]);
findbitwiseOR(A, N);
return 0;
}
Java
// Java program for the above approach
public class GFG {
// Function to find the Bitwise OR
// of Bitwise AND of all subarrays
static void findbitwiseOR(int[] a, int n)
{
// Stores the required result
int res = 0;
// Generate all the subarrays
for (int i = 0; i < n; i++) {
// Store the current element
int curr_sub_array = a[i];
// Find the Bitwise OR
res = res | curr_sub_array;
for (int j = i; j < n; j++) {
// Update the result
curr_sub_array = curr_sub_array & a[j];
res = res | curr_sub_array;
}
}
// Print the result
System.out.println(res);
}
// Driver code
public static void main(String[] args)
{
int A[] = { 1, 2, 3 };
int N = A.length;
findbitwiseOR(A, N);
}
}
// This code is contributed by abhinavjain194
Python3
# Python3 program for the above approach
# Function to find the Bitwise OR
# of Bitwise AND of all subarrays
def findbitwiseOR(a, n):
# Stores the required result
res = 0
# Generate all the subarrays
for i in range(n):
# Store the current element
curr_sub_array = a[i]
# Find the Bitwise OR
res = res | curr_sub_array
for j in range(i, n):
# Update the result
curr_sub_array = curr_sub_array & a[j]
res = res | curr_sub_array
# Print the result
print (res)
# Driver Code
if __name__ == '__main__':
A = [ 1, 2, 3 ]
N = len(A)
findbitwiseOR(A, N)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the Bitwise OR
// of Bitwise AND of all subarrays
static void findbitwiseOR(int[] a, int n)
{
// Stores the required result
int res = 0;
// Generate all the subarrays
for (int i = 0; i < n; i++) {
// Store the current element
int curr_sub_array = a[i];
// Find the Bitwise OR
res = res | curr_sub_array;
for (int j = i; j < n; j++) {
// Update the result
curr_sub_array = curr_sub_array & a[j];
res = res | curr_sub_array;
}
}
// Print the result
Console.Write(res);
}
// Driver code
static void Main()
{
int[] A = { 1, 2, 3 };
int N = A.Length;
findbitwiseOR(A, N);
}
}
// This code is contributed by sanjoy_62.
JavaScript
<script>
// JavaScript program for the above approach
// Function to find the Bitwise OR
// of Bitwise AND of all subarrays
function findbitwiseOR(a, n)
{
// Stores the required result
let res = 0;
// Generate all the subarrays
for (let i = 0; i < n; i++) {
// Store the current element
let curr_sub_array = a[i];
// Find the Bitwise OR
res = res | curr_sub_array;
for (let j = i; j < n; j++) {
// Update the result
curr_sub_array = curr_sub_array & a[j];
res = res | curr_sub_array;
}
}
// Print the result
document.write(res);
}
// Driver Code
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the Bitwise OR of
// Bitwise AND of all consecutive
// subsets of the array
void findbitwiseOR(int* a, int n)
{
// Stores the required result
int res = 0;
// Traverse the given array
for (int i = 0; i < n; i++)
res = res | a[i];
// Print the result
cout << res;
}
// Driver Code
int main()
{
int A[] = { 1, 2, 3 };
int N = sizeof(A) / sizeof(A[0]);
findbitwiseOR(A, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to find the Bitwise OR of
// Bitwise AND of all consecutive
// subsets of the array
static void findbitwiseOR(int[] a, int n)
{
// Stores the required result
int res = 0;
// Traverse the given array
for(int i = 0; i < n; i++)
res = res | a[i];
// Print the result
System.out.println(res);
}
// Driver Code
public static void main(String[] args)
{
int[] A = { 1, 2, 3 };
int N = A.length;
findbitwiseOR(A, N);
}
}
// This code is contributed by Dharanendra L V.
Python3
# Python3 program for the above approach
# Function to find the Bitwise OR of
# Bitwise AND of all consecutive
# subsets of the array
def findbitwiseOR(a, n):
# Stores the required result
res = 0
# Traverse the given array
for i in range(n):
res = res | a[i]
# Print the result
print(res)
# Driver Code
if __name__ == '__main__':
A = [ 1, 2, 3 ]
N = len(A)
findbitwiseOR(A, N)
# This code is contributed by ipg2016107
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the Bitwise OR of
// Bitwise AND of all consecutive
// subsets of the array
static void findbitwiseOR(int[] a, int n)
{
// Stores the required result
int res = 0;
// Traverse the given array
for(int i = 0; i < n; i++)
res = res | a[i];
// Print the result
Console.Write(res);
}
// Driver Code
public static void Main()
{
int[] A = { 1, 2, 3 };
int N = A.Length;
findbitwiseOR(A, N);
}
}
// This code is contributed by ukasp
JavaScript
<script>
// JavaScript program for the above approach
// Function to find the Bitwise OR of
// Bitwise AND of all consecutive
// subsets of the array
function findbitwiseOR(a, n)
{
// Stores the required result
var res = 0;
var i;
// Traverse the given array
for (i = 0; i < n; i++)
res = res | a[i];
// Print the result
document.write(res);
}
// Driver Code
var A = [1, 2, 3];
var N = A.length;
findbitwiseOR(A, N);
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Bitwise OR of Bitwise AND of all subsets of an Array for Q queries Given two arrays arr[] of size N and queries[] of size Q, the task is to find the OR of AND of subsets of the array. In each query, you are given an index and a value, you have to replace the value at the given index of the arrays with a given value and print the OR of AND of all subsets of the arra
9 min read
Bitwise OR of sum of all subsequences of an array Given an array arr[] of length N, the task is to find the Bitwise OR of the sum of all possible subsequences from the given array. Examples: Input: arr[] = {4, 2, 5}Output: 15Explanation: All subsequences from the given array and their corresponding sums:{4} - 4{2} - 2{5} - 5{4, 2} - 6{4, 5} - 9{2,
6 min read
Bitwise XOR of Bitwise AND of all pairs from two given arrays Given two arrays arr1[] and arr2[] consisting of N and M integers respectively, the task is to print the Bitwise XOR of Bitwise AND of all pairs possible by selecting an element from arr1[] and arr2[]. Examples: Input: arr1[] = {1, 2, 3}, arr2[] = {6, 5}Output: 0Explanation: Bitwise AND of the pair
10 min read
Sum of bitwise AND of all subarrays Given an array consisting of N positive integers, find the sum of bit-wise and of all possible sub-arrays of the array. Examples: Input : arr[] = {1, 5, 8} Output : 15 Bit-wise AND of {1} = 1 Bit-wise AND of {1, 5} = 1 Bit-wise AND of {1, 5, 8} = 0 Bit-wise AND of {5} = 5 Bit-wise AND of {5, 8} = 0
8 min read
Sum of bitwise OR of all subarrays Given an array of positive integers, find the total sum after performing the bit wise OR operation on all the sub arrays of a given array. Examples: Input : 1 2 3 4 5 Output : 71 Input : 6 5 4 3 2 Output : 84 First initialize the two variable sum=0, sum1=0, variable sum will store the total sum and,
5 min read