Geeksforgeeks

Timer is Running

Given an array **A** of **n** integers (always a power of 2) and **q **queries.

Every Query consists of two elements **index** and **value.** We need to write a program that assigns *the value* to

- 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

1

3

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.

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.

Author: vanshi_14

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.

jainshubham52535 | 529 |

Pathak1899 | 436 |

yashdip123 | 380 |

kirtidee18 | 377 |

jots62 | 351 |

kirtidee18 | 1057 |

ykaushik | 752 |

aggarwalvatsal5 | 741 |

harbilassingh | 576 |

pmr_3689 | 570 |

blackshadows | 5456 |

Ibrahim Nash | 5450 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4827 |

Login to report an issue on this page.