I was asked this question on an interview coding assignment. While i was unsuccessful to complete this task, I was wondering if anyone can help me understand how this can be done.
Question:
You are given number of nodes and edges. With that you are given two arrays: g_From[]
and g_To[]
. you are to find the maximum distance between two nodes.
Input:
First line consists of two integers, Number of nodes n and edges e.
Next e lines consists of g_From and g_To
example:
5 6 //Number of Nodes = 5, edges = 6. Following 6 line are to-from
1 2 //g_from[0] = 1, g_to[0] = 2 => from node 1 to node 2
1 3 //g_from[1] = 1, g_to[1] = 3 => from node 1 to node 3
2 3 //g_from[2] = 2, g_to[2] = 3 => from node 2 to node 3
2 4 //g_from[3] = 2, g_to[3] = 4 => from node 2 to node 4
3 4 //g_from[4] = 3, g_to[4] = 4 => from node 3 to node 4
4 5 //g_from[5] = 4, g_to[5] = 5 => from node 4 to node 5
Output:
4
Explanation:
As you can see from the inputs there are many ways to traverse
for example:
1->3->4->5
2->3->4->5
but the longest route is: 1->2->3->4->5
The distance is 5-1 = 4, answer!