Bitwise OR of Bitwise AND of all subsets of an Array for Q queries
Last Updated :
28 Jun, 2022
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 array after each query.
Examples:
Input: arr[] = {3, 5, 7}, N = 3, queries[] = {{1, 2}, {2, 1}}, Q = 2
Output: 7
3
Explanation:
For the first query replace the value at index 1 with value 2, updated array arr[] is {3, 2, 7}.
Then take AND of all subsets i.e. AND(3) = 3, AND(2) = 2, AND(7) = 7, AND(3, 2) = 2, AND(3, 7) = 3, AND(3, 2, 7) = 2.
OR of the AND of all subsets are OR(3, 2, 7, 2, 3, 2) = 7.
Now, for the second query replace the value at index 2 with value 1, updated array arr[] is {3, 2, 1}.
Then take AND of all subsets i.e. AND(3) = 3, AND(2) = 2, AND(1) = 1, AND(3, 2) = 2, AND(3, 1) = 1, AND(3, 2, 1) = 0.
OR of the AND of all subsets OR(3, 2, 1, 2, 1, 0) = 3.
Input: arr[] = {1, 2, 3}, N = 3, queries[] = {{2, 4}, {1, 8}}, Q = 2
Output: 7
13
Approach: This problem can be solved using greedy algorithm. Follow the steps below to solve the problem:
- Initialize an array bits[] of size 32 and store the count of set bits of all the elements.
- Iterate in the range [0, Q-1] using the variable p:
- First subtract the bits of previous value and then add the bits of new value.
- Iterate in the range [0, 31] using the variable i:
- If the current bit is set for the previous value, then subtract one bit to the bits[] array at ith index.
- If the current bit is set for the new value, then add one bit to the bits[] array at ith index.
- Replace the new value from the previous value in given array arr[].
- Initialize a variable ans to store the OR of the bits array.
- Iterate in the range [0, 31] using the variable i:
- If the current bit is not equal to 0, then add the OR of the current bit into ans.
- After completing the above steps, print the ans as the required answer for each query.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void Or_of_Ands_for_each_query( int arr[], int n,
int queries[][2], int q)
{
int bits[32] = { 0 };
for ( int i = 0; i < 32; i++) {
for ( int j = 0; j < n; j++) {
if ((1 << i) & arr[j]) {
bits[i]++;
}
}
}
for ( int p = 0; p < q; p++) {
for ( int i = 0; i < 32; i++) {
if ((1 << i) & arr[queries[p][0]]) {
bits[i]--;
}
if (queries[p][1] & (1 << i)) {
bits[i]++;
}
}
arr[queries[p][0]] = queries[p][1];
int ans = 0;
for ( int i = 0; i < 32; i++) {
if (bits[i] != 0) {
ans |= (1 << i);
}
}
cout << ans << endl;
}
}
int main()
{
int n = 3, q = 2;
int arr[] = { 3, 5, 7 };
int queries[2][2] = { { 1, 2 }, { 2, 1 } };
Or_of_Ands_for_each_query(arr, n, queries, q);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void Or_of_Ands_for_each_query( int arr[], int n,
int queries[][],
int q)
{
int bits[] = new int [ 32 ];
Arrays.fill(bits, 0 );
for ( int i = 0 ; i < 32 ; i++) {
for ( int j = 0 ; j < n; j++) {
if ((( 1 << i) & arr[j]) != 0 ) {
bits[i]++;
}
}
}
for ( int p = 0 ; p < q; p++) {
for ( int i = 0 ; i < 32 ; i++) {
if ((( 1 << i) & arr[queries[p][ 0 ]]) != 0 ) {
bits[i]--;
}
if ((queries[p][ 1 ] & ( 1 << i)) != 0 ) {
bits[i]++;
}
}
arr[queries[p][ 0 ]] = queries[p][ 1 ];
int ans = 0 ;
for ( int i = 0 ; i < 32 ; i++) {
if (bits[i] != 0 ) {
ans |= ( 1 << i);
}
}
System.out.println(ans);
}
}
public static void main(String[] args)
{
int n = 3 , q = 2 ;
int arr[] = { 3 , 5 , 7 };
int queries[][] = { { 1 , 2 }, { 2 , 1 } };
Or_of_Ands_for_each_query(arr, n, queries, q);
}
}
|
Python3
def Or_of_Ands_for_each_query(arr, n, queries, q):
bits = [ 0 for x in range ( 32 )]
for i in range ( 0 , 32 ):
for j in range ( 0 , n):
if (( 1 << i) & arr[j]):
bits[i] + = 1
for p in range ( 0 , q):
for i in range ( 0 , 32 ):
if (( 1 << i) & arr[queries[p][ 0 ]]):
bits[i] - = 1
if (queries[p][ 1 ] & ( 1 << i)):
bits[i] + = 1
arr[queries[p][ 0 ]] = queries[p][ 1 ]
ans = 0
for i in range ( 0 , 32 ):
if (bits[i] ! = 0 ):
ans | = ( 1 << i)
print (ans)
n = 3
q = 2
arr = [ 3 , 5 , 7 ]
queries = [[ 1 , 2 ], [ 2 , 1 ]]
Or_of_Ands_for_each_query(arr, n, queries, q)
|
C#
using System;
class GFG{
static void Or_of_Ands_for_each_query( int [] arr, int n,
int [,] queries,
int q)
{
int [] bits = new int [32];
for ( int i = 0; i < 32; i++)
{
bits[i] = 0;
}
for ( int i = 0; i < 32; i++)
{
for ( int j = 0; j < n; j++)
{
if (((1 << i) & arr[j]) != 0)
{
bits[i]++;
}
}
}
for ( int p = 0; p < q; p++)
{
for ( int i = 0; i < 32; i++)
{
if (((1 << i) & arr[queries[p, 0]]) != 0)
{
bits[i]--;
}
if ((queries[p, 1] & (1 << i)) != 0)
{
bits[i]++;
}
}
arr[queries[p, 0]] = queries[p, 1];
int ans = 0;
for ( int i = 0; i < 32; i++)
{
if (bits[i] != 0)
{
ans |= (1 << i);
}
}
Console.WriteLine(ans);
}
}
public static void Main(String[] args)
{
int n = 3, q = 2;
int [] arr = { 3, 5, 7 };
int [,] queries = { { 1, 2 }, { 2, 1 } };
Or_of_Ands_for_each_query(arr, n, queries, q);
}
}
|
Javascript
<script>
function Or_of_Ands_for_each_query(arr, n, queries, q) {
let bits = new Array(32).fill(0);
for (let i = 0; i < 32; i++) {
for (let j = 0; j < n; j++) {
if ((1 << i) & arr[j]) {
bits[i]++;
}
}
}
for (let p = 0; p < q; p++) {
for (let i = 0; i < 32; i++) {
if ((1 << i) & arr[queries[p][0]]) {
bits[i]--;
}
if (queries[p][1] & (1 << i)) {
bits[i]++;
}
}
arr[queries[p][0]] = queries[p][1];
let ans = 0;
for (let i = 0; i < 32; i++) {
if (bits[i] != 0) {
ans |= (1 << i);
}
}
document.write(ans + "<br>" );
}
}
let n = 3, q = 2;
let arr = [3, 5, 7];
let queries = [[1, 2],
[2, 1]];
Or_of_Ands_for_each_query(arr, n, queries, q);
</script>
|
Time Complexity: O(max(N, Q))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...