Minimum Flips to Match Expected Binary Values in a Tree
Updated
Problem statement
You are given a tree with tree_nodes nodes, numbered 0 to tree_nodes - 1. Node 0 is the root. The i-th edge joins tree_from[i] and tree_to[i].
Every node holds a value, 0 or 1. The array initial gives the starting values. The array expected gives the values you want.
In one operation you pick a node u. The operation flips the value of u. It also flips the value of every node v in the subtree of u where v % 2 == u % 2. Here v and u are node numbers, so only nodes whose numbers are both even or both odd are flipped together. Flipping means changing 0 to 1 or 1 to 0. The subtree of u is u and everything below it when the tree hangs from node 0.
You can do as many operations as you like. Return the smallest number of operations that makes the values match expected.
For example, take the path 0 - 1 - 2 with initial = [0, 0, 0] and expected = [1, 0, 1]. Picking node 0 flips nodes 0 and 2, so one operation is enough.
Function
minimumFlipsToMatch(tree_nodes: int, tree_from: int[], tree_to: int[], initial: int[], expected: int[]) → intExamples
Example 1
tree_nodes = 4tree_from = [0, 0, 1]tree_to = [1, 2, 3]initial = [1, 1, 0, 1]expected = [0, 1, 1, 0]return = 2The edges are 0 - 1, 0 - 2 and 1 - 3. The values start as [1, 1, 0, 1] and should become [0, 1, 1, 0].
Pick node 0. It flips nodes 0 and 2, which are the even nodes in its subtree. The values are now [0, 1, 1, 1].
Pick node 3. It flips only itself. The values are now [0, 1, 1, 0], which matches expected. That took 2 operations, and fewer is not possible.
Example 2
tree_nodes = 5tree_from = [0, 1, 0, 1]tree_to = [1, 2, 3, 4]initial = [1, 0, 1, 1, 0]expected = [1, 1, 0, 1, 1]return = 3The edges are 0 - 1, 1 - 2, 0 - 3 and 1 - 4. The values start as [1, 0, 1, 1, 0] and should become [1, 1, 0, 1, 1].
Node 1 is wrong. Pick it. Its subtree holds nodes 1, 2 and 4, and only node 1 is odd, so only it flips: [1, 1, 1, 1, 0].
Node 2 is now wrong. Pick it. It flips only itself: [1, 1, 0, 1, 0].
Node 4 is still wrong. Pick it: [1, 1, 0, 1, 1]. That matches expected after 3 operations.
Constraints
1 ≤ tree_nodes ≤ 105tree_fromandtree_toboth havetree_nodes - 1numbers, and every pairtree_from[i],tree_to[i]is an edge. All edges together form a tree.0 ≤ tree_from[i], tree_to[i] < tree_nodesinitialandexpectedeach havetree_nodesvalues, and every value is0or1
More UKG interview questions
Similar questions at other companies
- Binary Tree CamerasAmazon · OA · PHONE SCREEN
- Collect Opportunity Data in a TreeSalesforce · OASalesforce interview questions
- Collect CoinsCisco · OACisco interview questions
- Uber Circular Shuttle OptimizationUber · OAUber interview questions
- Save the UniverseRubrik · OARubrik interview questions
- Equalize Server LatencyMicrosoft · OAMicrosoft interview questions
- Equalize Server LatencyTikTok · OATikTok interview questions
- Limit an Organization Tree's HeightRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Minimum Leaf Path Sum over All RootsInfosys · OAInfosys interview questions
- Part 2: Batch Access ReviewScale AI · OAScale AI interview questions