Element left after performing alternate OR & XOR operation | Practice | GeeksforGeeks

Given an array A of N integers and a 2D matrix denoting q queries. Each query consists of two elements, index and value. Update value at index in A for each query and then perform the following operations to get the result for that query.
1. Perform bitwise OR on each pair 
2. Perform bitwise XOR on each pair 
Do this alternately till you are left with only a single element in A. 

Example 1:
Input: 
N = 4
A = {1, 4, 5, 6}
q = 2
query = {{0, 2}, {3, 5}}

Output: 1 3

Explaination: 
1st query: 
Update the value of A[0] to 2 as given in 
the query pair.The array becomes {2, 4, 5, 6}.
1st iteration: Perform bitwise OR on pairs 
{2,4} and {5,6}. The array becomes {6,7}.
2nd iteration: Perform bitwise XOR on pairs 
{6,7}. The array becomes {1}.


2nd query: 
Update the value of A[3] to 5 as given in 
the query pair. The array becomes {2, 4, 5, 5}.
1st iteration: Perform bitwise OR on pairs 
{2,4} and {5,5}. The array becomes {6,5}.
2nd iteration: 6^5=3. The array becomes {3}.



Your Task:
You do not need to read input or print anything. Your task is to complete the function left() which takes N, A[], q and query as input parameters and returns a list of integers denoting the result for each query.

Expected Time Complexity:O(q*logN)
Expected Auxiliary Space: O(N)

Constraints:
1 ≤ N ≤ 105
1 ≤ A[i] ≤ 105
1 ≤ q ≤ 104
0
vjvish982 months ago
//can anyone tell me what's wrong in this code.. plz
?????????????
    vector<int> left(int N, int A[], int q, vector<pair<int, int>> query)
   {
       vector<int>v;
       vector<int>b;
       for(int i=0;i<N;i++)
       {
         b.push_back(A[i]);
       }
       for(int i=0;i<q;i++)
       {  
          int x=0;
          int ind=query[i].first;
          int val=query[i].second;
          b[ind]=val;
          for(int j=1;j<b.size();)
          {
             x^=b[j]|b[j-1];
             j++;j++;
          }
          v.push_back(x);
       }
       return v;
   }
 
 
 
 
 
 
 
 
0
kaditya213 months ago
My answer is correct, there is issue in the driver code which is responsible for printing.
0
hemant3 years ago
hemant
Here is my Solution using Segment Tree:https://ide.geeksforgeeks.o...Time complexity: O(Q*log(n))Space complexity: O(n)
0
Rahul Tandekar3 years ago
Rahul Tandekar
please help me how can i apply bitwise | on first 2 elements in  given arrayA[]={1,4,3,6}
0
manav 3 years ago
manav 
https://ide.geeksforgeeks.o...
please tell me what is wrong with this code.
0
yogesh kothari4 years ago
yogesh kothari
Here is my solution of complexity O ( Q * log n)where q=no. of querieshttps://ide.geeksforgeeks.o...A simple array implementation of binary tree
0
Johan Reji4 years ago
Johan Reji
Your program took more time than expected.Time Limit ExceededExpected Time Limit < 1.32sec
Please help me optimize my code
https://ide.geeksforgeeks.o...
0
pagala_bhanra4 years ago
pagala_bhanra
import java.util.*;import java.lang.*;import java.io.*;
class segment_tree{    int []input;    int []seg;    int n;    segment_tree(int []inp)    {        n = inp.length;        input = new int[n];        int x = (int) (Math.ceil(Math.log(n) / Math.log(2)));        int max_size = 2 * (int) Math.pow(2, x) - 1;        seg = new int[max_size];        for(int i=0;i<n;i++) input[i]="inp[i];" int="" pow="(int)(Math.log(n)/Math.log(2));" if(pow%2="=" 0)="" construct_tree(0,n="" -="" 1,0,1);="" exor="" else="" construct_tree(0,n="" -="" 1,0,0);="" or="" }="" public="" void="" construct_tree(int="" l,int="" h,int="" pos,int="" op)="" {="" if(l="=h)" seg[pos]="input[l];" else="" {="" int="" mid="(l+h)/2;" construct_tree(l,mid,2*pos="" +="" 1,op^1);="" construct_tree(mid+1,h,2*pos="" +="" 2,op^1);="" if(op="=" 0)="" seg[pos]="seg[2*pos" +="" 1]="" |="" seg[2*pos="" +="" 2]="" ;="" else="" seg[pos]="seg[2*pos" +="" 1]="" ^="" seg[2*pos="" +="" 2]="" ;="" }="" }="" public="" void="" update(int="" index,int="" val,int="" node,int="" se,int="" ss,int="" op)="" {="" if(index="" <="" se="" ||="" index=""> ss)        return;        if(se == ss)        {            seg[node] = val;            return;        }        int mid = (se + ss)/2;        update(index,val,2*node + 1,se,mid,op^1);        update(index,val,2*node + 2,mid + 1,ss,op^1);        if(op == 0)            seg[node] = seg[2*node + 1] | seg[2*node + 2] ;        else            seg[node] = seg[2*node + 1] ^ seg[2*node + 2] ;    }public static void main (String[] args) {//codeScanner in = new Scanner(System.in);int t = in.nextInt();while(t-->0){    int n = in.nextInt();    int []ar = new int[n];    for(int i=0;i<n;i++) ar[i]="in.nextInt();" segment_tree="" st="new" segment_tree(ar);="" int="" temp="(int)(Math.log(n)/Math.log(2));" int="" base_op="0;" if(temp%2="=" 0)="" base_op="1;" else="" base_op="0;" int="" m="in.nextInt();" while(m--="">0)    {        int ind = in.nextInt();        int val = in.nextInt();        if(ind >= n || ind < 0)        System.out.println("-1");        else        {        st.update(ind,val,0,0,n - 1,base_op);        System.out.println(st.seg[0]);        }    }}}}
0
Vijil Vijay4 years ago
Vijil Vijay
https://ide.geeksforgeeks.o...
0
Anshul Sharma4 years ago
Anshul Sharma
https://ide.geeksforgeeks.o...
Please check it. Every input is giving correct output but when I submit it, it says ArrayIndexOutOfBoundsException 1, though it will not come.
Please check it.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.