Find a node which is reachable from all other nodes | Phone
Anonymous User
1176

Given two arrays A & B where A[i] is connected to B[i], find a node which is reachable from all other nodes. If there are more than one such nodes or no such nodes, then return -1.
Assumptions:
Numbers in array will be in range of [0 - N], where is N is the length of the array.
Network of nodes is connected.

Examples:
A = [0,1,2,4,5], B = [2,3,3,3,2]
Output - 3 [Exaplanation - 0->2, 1->3, 2->3, 4->3, 5->2, here only node 3 does not have connection going out from it]

A = [2,3,3,4], B = [1,1,0,0]
Output : -1 [Exaplanation - there are 2 nodes which do not have any outgoing connection from it, hence output is -1]

What would be your approach to solve this question?

Comments (2)