Top Coding Interview Question – Solved

10 Live
Flip Operations A tree of n nodes is an undirected connected graph Hnving (n - 1) edges. Given an undirected tree comprising of tree_nodes number of nodes numbered from 0 to tree_nodes - 1, and rooted at node 0. Each node has an initially binary value (0 or 1) represented by the array initial[], and an expected binary value represented by the array expected[]. The following operation can be performed on the tree any number of times: - Pick a node u and flip its value. Also, flip the value of all the nodes vin the subtree of u if v % 2 = u %2. A node vlies in the subtree of u if ulies on the path from vto root. Flipping the value of a node means flipping its binary value, i.e., from 0 to 1, and vice versa. - The tree is represented by the arrays tree_from[] and tree_to[] where tree_from[i] is connected to tree_to[i]via an edge for 0 ≀ i< tree_nodes - 1. Find the minimum number of operations needed such that the final binary value of each node becomes equal to the array expected[]. Example Consider the following: tree_nodes = 4 initial = [1, 1, 0, 1] expected = [0, 1, 1, 0] tree_from = [0, 0, 1] tree_to = [1, 2, 3] The nodes are labeled <node number> :< value>.

Asked in: No companies listed

Image of the Question

Question Image Question Image

All Testcases Passed βœ”



Passcode Image

Solution


Please login to view the solution


Related Questions

| Given an n x m grid, where rows are numbered from 7 to n and columns from 1 to … |
| There are 'N' coders standing in a line, where i denotes the ith position of a … |
| A birthday party was attended by N number of kids, and each kid was given a uni… |
| Given a matrix of size m * n, where m denotes the number of rows (starting with… |
| A traveler is traveling from the city of Zeta to Omega. He starts with X amount… |
| As an operations engineer at Amazon, you are responsible for organizing the dis… |