ShareChat Coding Question – Solved

8 Live
Efficient Network There are n computer systems connected together to form a network. The network can be represented as a rooted tree (rooted at master computer 1), where the connections are described using two arrays connect_from[]and connect_to[]. Each pair (connect_from[i], connect_to[i]) denotes an undirected edge between the two computers. Additionally, each computer has a value assigned to it denoted by the array computer_val[]. In order to maximize the throughput of the network, certain inefficient systems can be removed. In one operation, any computer node can be chosen, and all computer nodes in its subtree including this node can be removed from the network. Let the number of such operations appfied be num_ops. For a given parameter k, the efficiency of the network after num_ops operations is calculated as: (sum of values of all remaining computer nodes - k * num_ops). Find the maximum possible efficiency after applying some (possibly zero) operations optimally. Note: The node values can be negative. Example Consider the number of computers to be connect_nodes = 4, and their connections to be connect_from = [1, 2, 3], connect_to = [2, 3, 4]. Also, consider the node values to be computer_val = [3, -7, -8, -9] and the given parameter to be k = 5. The given network cån be represented as:

Asked in: ShareChat

Image of the Question

Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


from collections import defaultdict
def getMaximumEfficiency(connect_nodes, connect_from, connect_to, computer_val, k):
    h = defaultdict(set)
    for i,j in zip(connect_from, connect_to):
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To solve this problem, you need to approach it from the perspective of optimizing a trade-off: retaining nodes in the network gives you the benefit of their values (which can be positive or negative), but removing entire subtrees allows you to reduce the negative impact of inefficient parts of the network at the cost of a fixed penalty for each removal operation.

Start by conceptualizing the problem in the form of a tree, where the network is rooted at node 1. The connections form a tree structure, meaning every node except the root has exactly one parent. Your goal is to maximize the final "efficiency" of the system, which is defined as the total sum of remaining node values minus the cost `k` multiplied by the number of subtree removal operations you decide to apply.

The key observation is that you’re allowed to remove any node and its entire subtree in one operation, and the cost is constant per operation, regardless of how many nodes are removed. Therefore, it’s worthwhile to consider removing a subtree only if doing so improves the overall efficiency — that is, if the sum of values in that subtree is so negative that removing it (despite the operation cost) increases the net gain.

The first step is to construct the tree structure from the given edges. Once the tree is built, you should process it in a bottom-up fashion (post-order traversal). This means that you start evaluating from the leaves up toward the root. This is important because, to decide whether to remove a node, you need to already know the impact of its children — whether they contribute positively or negatively to the overall subtree.

Now, for each node, you’ll want to calculate two things:
1. The total sum of values in the subtree rooted at this node (including the node itself and all its descendants that haven't been removed).
2. The optimal number of removals (operations) needed in this subtree to maximize the efficiency — that is, the best strategy of keeping or removing parts of this subtree.

At every node during traversal, you look at the results from each child. You accumulate the values from child subtrees, considering whether it is better to keep or remove each child. If a child subtree has a strongly negative total value, it may be beneficial to remove that entire subtree, paying the operation cost `k`, and not include its negative value in your current node’s sum.

After evaluating all children, you determine the best course of action for the current node. You now have a choice: either keep this node and its selected child subtrees (some possibly removed, some retained), or remove this entire subtree (current node and all children) in one operation.

So you compute two efficiency values:
- One if you keep the node (with adjusted child subtree sums and their associated best operations).
- Another if you remove the entire subtree (value becomes 0, and you add one operation cost).

You then compare which of the two options results in a better (higher) efficiency for this subtree. You pass the result upward so that the parent node can make its own decision with full information about each of its subtrees.

This recursive strategy ensures that at each level, you're making an optimal local decision based on the best decisions made in child subtrees. Eventually, at the root node (node 1), you’ll know the best possible efficiency that can be obtained for the entire tree — which is what the problem is asking for.

Edge cases to consider:
- All node values are negative: In this case, it might be optimal to remove large portions or even the entire tree, depending on how `k` compares to the magnitude of the node values.
- All values are positive: No operations should be performed because removing any node would reduce the total value and add an extra cost.
- Mixtures of positive and negative values: You need to carefully assess which subtrees contribute negatively enough to justify removal.

In conclusion, the essence of this problem is dynamic programming on trees. You need to make recursive decisions for each subtree, comparing the efficiency of keeping it versus removing it. By following a bottom-up strategy and always choosing the option with the highest efficiency at each step, you ensure that the final answer is globally optimal.
```


Related Questions