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++ program for the above approach #include<bits/stdc++.h> using namespace std;
// Function to find the Bitwise OR
// of Bitwise AND of all possible
// subarrays after performing the
// every query
void performQuery(vector< int > arr,
vector<vector< int >> Q)
{
// Traversing each pair
// of the query
for ( int i = 0; i < Q.size(); i++) {
// Stores the Bitwise OR
int or1 = 0;
// Updating the array
int x = Q[i][0];
arr[x - 1] = Q[i][1];
// Find the Bitwise OR of
// new updated array
for ( int j = 0; j < arr.size(); j++) {
or1 = or1 | arr[j];
}
// Print the ans
cout<<or1<< " " ;
}
}
// Driver Code
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);
}
// This code is contributed by ipg2016107. |
// Java program for the above approach import java.io.*;
class GFG {
// Function to find the Bitwise OR
// of Bitwise AND of all possible
// subarrays after performing the
// every query
static void performQuery( int arr[],
int Q[][])
{
// Traversing each pair
// of the query
for ( int i = 0 ; i < Q.length; i++) {
// Stores the Bitwise OR
int or = 0 ;
// Updating the array
int x = Q[i][ 0 ];
arr[x - 1 ] = Q[i][ 1 ];
// Find the Bitwise OR of
// new updated array
for ( int j = 0 ; j < arr.length; j++) {
or = or | arr[j];
}
// Print the ans
System.out.print(or + " " );
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 };
int Q[][] = { { 1 , 4 }, { 3 , 0 } };
performQuery(arr, Q);
}
} |
# Python program for the above approach # Function to find the Bitwise OR # of Bitwise AND of all possible # subarrays after performing the # every query def performQuery(arr, Q):
# Traversing each pair
# of the query
for i in range ( 0 , len (Q)):
# Stores the Bitwise OR
orr = 0
# Updating the array
x = Q[i][ 0 ]
arr[x - 1 ] = Q[i][ 1 ]
# Find the Bitwise OR of
# new updated array
for j in range ( 0 , len (arr)):
orr = orr | arr[j]
# Print the ans
print (orr ,end = " " )
# Driver Code arr = [ 1 , 2 , 3 ]
Q = [[ 1 , 4 ] , [ 3 , 0 ]]
performQuery(arr, Q) # This code is contributed by shivanisinghss2110 |
// C# program for the above approach using System;
class GFG {
// Function to find the Bitwise OR
// of Bitwise AND of all possible
// subarrays after performing the
// every query
static void performQuery( int []arr, int [,]Q)
{
// Traversing each pair
// of the query
for ( int i = 0; i < Q.Length; i++) {
// Stores the Bitwise OR
int or = 0;
// Updating the array
int x = Q[i,0];
arr[x - 1] = Q[i,1];
// Find the Bitwise OR of
// new updated array
for ( int j = 0; j < arr.Length; j++) {
or = or | arr[j];
}
// Print the ans
Console.Write(or + " " );
}
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 1, 2, 3 };
int [,]Q = { { 1, 4 }, { 3, 0 } };
performQuery(arr, Q);
}
} // This code is contributed by shivanisinghss2110 |
<script> // JavaScript Program to implement
// the above approach
// Function to find the Bitwise OR
// of Bitwise AND of all possible
// subarrays after performing the
// every query
function performQuery(arr, Q)
{
// Traversing each pair
// of the query
for (let i = 0; i < Q.length; i++) {
// Stores the Bitwise OR
let or = 0;
// Updating the array
let x = Q[i][0];
arr[x - 1] = Q[i][1];
// Find the Bitwise OR of
// new updated array
for (let j = 0; j < arr.length; j++) {
or = or | arr[j];
}
// Print the ans
document.write(or + " " );
}
}
// Driver Code
let arr = [1, 2, 3];
let Q = [[1, 4], [3, 0]];
performQuery(arr, Q);
// This code is contributed by Potta Lokesh </script>
|
7 6
Time Complexity: O(N*Q)
Auxiliary Space: O(1)