Element left after performing alternate OR & XOR operation
Submissions: 823   Accuracy:

18.43%

  Difficulty: Medium   Marks: 4

Given an array A of n integers (always a power of 2) and queries.
Every Query consists of two elements index and value. We need to write a program that assigns the value to A
index and prints the single element which is left after performing the following operations for each query:

  • At alternate steps perform bitwise OR and bitwise XOR operations to the adjacent elements.
  • In first iteration select, select n/2 pairs moving from left to right, and do a bitwise OR of all the pair values. In second iteration select (n/2)/2 leftover pairs and do a bitwise XOR on them. In the third iteration select, select ((n/2)/2)/2 leftover pairs moving from left to right, and do a bitwise OR of all the pair values.
  • Continue the above steps till we are left with a single element.

Input:
The first line contains an integer T,  the number of test cases. For each test case, the first line contains an integer n,  size of the array. Next line contains n space separated integers. Next line contains an integer q, number of queries. Next q line contains 2 integers index and value. 

Output:
For each test case, the output is the element left after performing the query if possible else print -1.

Constraints:
1<=T<=100
1<=N<=10^5

0<=A[i]<=10^5
0<=index < N
1<=Q<=10^4

Example:
Input

1
4
1 4 5 6
2
0 2
3 5
Output
1
3
Explanation:
1st query:
Assigning 2 to index 0, the sequence is now [2, 4, 5, 6]. 1st iteration: There are 4/2=2 pairs (2, 4) and (5, 6) 2 OR 4 gives 6, and 5 OR 6 gives us 7.
So the sequence is now [6, 7]. 2nd iteration: There is 1 pair left now (6, 7) 6^7=1. Hence the last element left is 1 which is the answer to our first query.

2nd Query:
Assigning 5 to index 3, the sequence is now [2, 4, 5, 5]. 1st iteration: There are 4/2=2 pairs (2, 4) and (5, 5) 2 OR 4 gives 6, and 5 OR 5 gives us 5. So the sequence is now [6, 5]. 2nd iteration: There is 1 pair left now (6, 5) 6^5=3. Hence the last element left is 3 which is the answer to our second query.

 

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Vanshika
Author: Vanshika_pec


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.