Problem Β· Tree

Maximum Time Required to Transfer Data

Learn this problem
● MediumRipplingNEW GRADOA

Problem statement

A server network is represented as a tree of g_nodes servers indexed from 1 to g_nodes. It has g_nodes - 1 edges, where the ith edge connects servers g_from[i] and g_to[i]. The transfer time between any two connected servers is 1 unit.

Given the graph g, find the maximum time taken to transfer data between any two servers in the system.

Function

maximumTimeRequiredToTransferData(g_nodes: int, g_from: int[], g_to: int[]) β†’ int

Examples

Example 1

g_nodes = 3g_from = [1, 2]g_to = [2, 3]return = 2

The longest path is from server 1 to server 3. It contains two edges, so the maximum transfer time is 2.

Example 2

g_nodes = 5g_from = [1, 1, 1, 5]g_to = [5, 3, 2, 4]return = 3

A longest path is 4 β†’ 5 β†’ 1 β†’ 2 (or it may end at server 3). It contains three edges, so the maximum transfer time is 3.

More Rippling problems

drafts saved locally
public int maximumTimeRequiredToTransferData(int g_nodes, int[] g_from, int[] g_to) {
  // write your code here
}
g_nodes3
g_from[1, 2]
g_to[2, 3]
expected2
checking account