![]() |
VOOZH | about |
Given an integer N denoting the number of connected cities ( numbered from 1 to N ) and a 2D array arr[][] consisting of pairs connected to each other by bidirectional bridges. The task is to find the minimum the number of bridges required to be crossed to reach the city N from the city 1.
Examples:
Input: N = 3, M = 2, arr[][] = {{1, 2}, {2, 3}}
👁 Image
Output: 2
Explanation:
To reach Node 2 from Node 1, 1 bridge is required to be crossed.
To reach Node 3 from Node 2, 1 bridge is required to be crossed.
Hence, 2 bridges are required to be connected.Input: N = 4, M = 3, arr[][] = {{1, 2}, {2, 3}, {2, 4}}
Output: 2
Approach: Follow the steps below to solve the problem:
Below is the implementation of the above approach:
2
Time Complexity: O(N)
Auxiliary Space: O(N)