Bitwise OR of bitwise AND of all possible non-empty subarrays after Q query updates
Last Updated :
26 Aug, 2021
Given an array arr[] consisting of N positive integers and an array of queries Q[] of type [L, R], the task is to find the Bitwise OR of the bitwise AND of all the possible non-empty subarrays of the array after updating array element at index L to R for each query.
Examples:
Input: arr[ ] = {1, 2, 3}, Q[ ] = {{1, 4}, {3, 0}}
Output: 7 6
Explanation:
All subarrays are {1}, {2}, {3}, {1, 2}, {2, 3} and {1, 2, 3}
For query 1: Update arr[1] = 4, then new array is {4, 2, 3}. The bitwise & of all subarrays are 4, 2, 3, 0, 2, 0 and the bitwise OR( {4, 2, 3, 0, 2, 0}) equals 7.
For query 2: Update arr[3] = 0, then new array is {4, 2, 0}. The bitwise & of all subarrays are 4, 2, 0, 0, 0, 0 and the bitwise OR( {4, 2, 0, 0, 0, 0}) equals 6.
Input: arr[ ] = {1, 2, 1}, Q[ ] = {{2, 1}}
Output: 1
Naive Approach: The simplest approach to solve the problem is to traverse the array Q[] and for each query update the array element arr[L] to R and find all the subarrays and their bitwise AND and store them in the new array. After storing the Bitwise AND, find the bitwise OR of the new array formed.
Time Complexity: O(N2Q)
Auxiliary Space: O(N2)
Efficient Approach: The above approach can also be optimized by using the fact that the bitwise OR of the resulting bitwise AND of all the generated subarrays are the same as the resulting bitwise OR of all the elements present in the array.
Therefore, the idea is to perform the queries and print the value of Bitwise OR of all array elements after updating the array each time.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void performQuery(vector< int > arr,
vector<vector< int >> Q)
{
for ( int i = 0; i < Q.size(); i++) {
int or1 = 0;
int x = Q[i][0];
arr[x - 1] = Q[i][1];
for ( int j = 0; j < arr.size(); j++) {
or1 = or1 | arr[j];
}
cout<<or1<< " " ;
}
}
int main()
{
vector< int > arr({ 1, 2, 3 });
vector< int > v1({1,4});
vector< int > v2({3,0});
vector<vector< int >> Q;
Q.push_back(v1);
Q.push_back(v2);
performQuery(arr, Q);
}
|
Java
import java.io.*;
class GFG {
static void performQuery( int arr[],
int Q[][])
{
for ( int i = 0 ; i < Q.length; i++) {
int or = 0 ;
int x = Q[i][ 0 ];
arr[x - 1 ] = Q[i][ 1 ];
for ( int j = 0 ; j < arr.length; j++) {
or = or | arr[j];
}
System.out.print(or + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 };
int Q[][] = { { 1 , 4 }, { 3 , 0 } };
performQuery(arr, Q);
}
}
|
Python3
def performQuery(arr, Q):
for i in range ( 0 , len (Q)):
orr = 0
x = Q[i][ 0 ]
arr[x - 1 ] = Q[i][ 1 ]
for j in range ( 0 , len (arr)):
orr = orr | arr[j]
print (orr ,end = " " )
arr = [ 1 , 2 , 3 ]
Q = [[ 1 , 4 ] , [ 3 , 0 ]]
performQuery(arr, Q)
|
C#
using System;
class GFG {
static void performQuery( int []arr, int [,]Q)
{
for ( int i = 0; i < Q.Length; i++) {
int or = 0;
int x = Q[i,0];
arr[x - 1] = Q[i,1];
for ( int j = 0; j < arr.Length; j++) {
or = or | arr[j];
}
Console.Write(or + " " );
}
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3 };
int [,]Q = { { 1, 4 }, { 3, 0 } };
performQuery(arr, Q);
}
}
|
Javascript
<script>
function performQuery(arr, Q)
{
for (let i = 0; i < Q.length; i++) {
let or = 0;
let x = Q[i][0];
arr[x - 1] = Q[i][1];
for (let j = 0; j < arr.length; j++) {
or = or | arr[j];
}
document.write(or + " " );
}
}
let arr = [1, 2, 3];
let Q = [[1, 4], [3, 0]];
performQuery(arr, Q);
</script>
|
Time Complexity: O(N*Q)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...