Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
Given an array of N(always a power of 2) elements and Q queries.
Every Query consists of two elements index and value.. We need to write a program that assigns value to Aindex and prints the single element which is left after performing below 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 : n = 4 m = 2
arr = [1, 4, 5, 6]
1st: index=0 value=2
2nd: index=3 value=5
Output : 1
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)
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)
Hence the last element left is 3 which is the
answer to our second query.
Naive Approach: The naive approach is to perform every step till we are leftover with one element. Using 2-D vector we will store the resultant elements left after every step. V[steps-1][0..size] gives the number of elements at previous step. If the step number is odd, we perform a bitwise OR operation, else a bitwise XOR operation is done. Repeat the steps till we are left over with one element. The last element left will be our answer.
Below is the implementation of the naive approach:
Efficient Approach: The efficient approach is to use Segment tree. Below is the complete segment tree approach used to solve the problem.
Building the tree
The leaves of the segment tree will store the array values and their parent will store the OR of the leaves. Moving up-ward in the tree, with every alternate step, the parent stores either of bitwise XOR or bitwise OR of left and right child. At every odd-numbered iteration, we perform the bitwise OR of the pairs and similarly we perform bitwise XOR of pairs at every even-numbered operation. So the odd-numbered parent will store the bitwise OR of the left and right child. Similarly, the even-numbered parent stores the bitwise XOR of the left and right child. level is an array that stores levels of every parent starting from 1, to determine if the pair(right child and left child) below it performs an OR operation or an XOR operation. The root of the tree will be our answer to the given sequence after every update operation.
The image above explains the construction of the tree if the sequence was [1, 2, 3, 4, 5, 6, 7, 8], then after 3 iterations, we will be left over with 12 which is our answer and is stored at the root.
There is no need to rebuild the complete tree to perform an update operation. To do an update, we should find a path from the root to the corresponding leaf and recalculate the values only at the parents that are lying on the found path.
Level of parent:
Using DP on trees, we can easily store the level of every parent. Initialize the leaf nodes level to 0, and keep adding as we move up to every parent.
The recurrence relation for calculating the level of parent is:
level[parent] = level[child] + 1
Here, child is 2*pos + 1 or 2*pos + 2
Below is the implementation of the above approach:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.