304 North Cardinal St.
Dorchester Center, MA 02124

# Find Closest Node to Given Two Nodes LeetCode Solution

## Problem – Find Closest Node to Given Two Nodes LeetCode Solution

You are given a directed graph of `n` nodes numbered from `0` to `n - 1`, where each node has at most one outgoing edge.

The graph is represented with a given 0-indexed array `edges` of size `n`, indicating that there is a directed edge from node `i` to node `edges[i]`. If there is no outgoing edge from `i`, then `edges[i] == -1`.

You are also given two integers `node1` and `node2`.

Return the index of the node that can be reached from both `node1` and `node2`, such that the maximum between the distance from `node1` to that node, and from `node2` to that node is minimized. If there are multiple answers, return the node with the smallest index, and if no possible answer exists, return `-1`.

Note that `edges` may contain cycles.

Example 1:

``````Input: edges = [2,2,3,-1], node1 = 0, node2 = 1
Output: 2
Explanation: The distance from node 0 to node 2 is 1, and the distance from node 1 to node 2 is 1.
The maximum of those two distances is 1. It can be proven that we cannot get a node with a smaller maximum distance than 1, so we return node 2.``````

Example 2:

``````Input: edges = [1,2,-1], node1 = 0, node2 = 2
Output: 2
Explanation: The distance from node 0 to node 2 is 2, and the distance from node 2 to itself is 0.
The maximum of those two distances is 2. It can be proven that we cannot get a node with a smaller maximum distance than 2, so we return node 2.``````

Constraints:

• `n == edges.length`
• `2 <= n <= 105`
• `-1 <= edges[i] < n`
• `edges[i] != i`
• `0 <= node1, node2 < n`

### Find Closest Node to Given Two Nodes LeetCode Solution in C++

``````void bfs(int src, vector<int> &dist,vector<int>& edge,int n){
queue<int> q;
q.push(src);
dist[src]=0;
while(q.size()>0){
auto p= q.front(); q.pop();
if(edge[p]!=-1 and dist[edge[p]]==INT_MAX){
q.push(edge[p]);
dist[edge[p]]= dist[p]+1;
}
}
}

int closestMeetingNode(vector<int>& e, int n1, int n2) {
int n= e.size();
vector<int> A(n,INT_MAX), B(n,INT_MAX);
bfs(n1,A,e,n);
bfs(n2,B,e,n);
int res= INT_MAX, node=-1;
for(int i=0;i<n;i++){
if(A[i]==INT_MAX or B[i]==INT_MAX) continue;
if(res>max(A[i],B[i])) node=i,res= max(A[i],B[i]);
}
return node;
}
``````

### Find Closest Node to Given Two Nodes LeetCode Solution in Java

``````void bfs(int src, int[] dist,int[] edge,int n){
Queue<Integer> q= new LinkedList<>();
dist[src]=0;
while(q.size()>0){
var p= q.remove();
if(edge[p]!=-1 && dist[edge[p]]==Integer.INT_MAX){
dist[edge[p]]= dist[p]+1;
}
}
}

public int closestMeetingNode(int[] e, int n1, int n2) {
int n= e.length;
int[] A= new int[n];
int[] B= new int[n];
Arrays.fill(A,Integer.MAX_VALUE);
Arrays.fill(B,Integer.MAX_VALUE);

//call bfs
bfs(n1,A,e,n);
bfs(n2,B,e,n);

//get node with minimum value of distance
int res= Integer.MAX_VALUE, node=-1;
for(int i=0;i<n;i++){
if(A[i]==Integer.MAX_VALUE || B[i]==Integer.MAX_VALUE) continue;
if(res>Math.max(A[i],B[i])){
node=i;
res= Math.max(A[i],B[i]);
}
}
return node;
}
``````

### Find Closest Node to Given Two Nodes LeetCode Solution in Python

``````class Solution:
def closestMeetingNode(self, edges: List[int], node1: int, node2: int) -> int:

res = float("inf")

def dfs(node, arr, visited, counter=0):

#assigning how many moves it takes to reach node
arr[node] = counter

next_node = edges[node]
if next_node!=-1 and next_node not in visited:
#going through each neighbor if exists and updating the counter
dfs(edges[node], arr, visited, counter+1)

return arr

#find moves to reach nodes from node1
n1 = [-1 for i in range(len(edges))]
dfs(node1, n1, set())

#find moves to reach nodes from node1
n2 = [-1 for i in range(len(edges))]
dfs(node2, n2, set())

for i in range(len(edges)):

#check if the end node is reachable from both starting nodes
if n1[i]!=-1 and n2[i]!=-1:
maximum_distance = max(n1[i], n2[i])

#update the distance and the final answer if relevant
if maximum_distance<res:
res = maximum_distance

``````
##### Find Closest Node to Given Two Nodes LeetCode Solution Review:

In our experience, we suggest you solve this Find Closest Node to Given Two Nodes LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Find Closest Node to Given Two Nodes LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Find Closest Node to Given Two Nodes LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions