Unable to pass a testcase

What is wrong in this code?

You have declared a common variable d for distance and you keep updating it in every iteration of BFS. If two nodes which are at the same distance from the source node are in the queue, you update the distance for the one you pop later with += 6 which is wrong. You need to maintain a distance array in BFS and update the distance using distance[adjacent_node] = distance[popped_node] + 6.

1 Like