Geeksforgeeks

Error

Given a graph with **V** vertices. Find the number of Provinces.

**Note: ** A province is a group of directly or indirectly connected cities and no other cities outside of the group.

**Example 1:**

Input:Output:2Explanation:The graph clearly has 2 Provinces.

Input:Output :1

**Your Task: **

You don't need to read input or print anything. Your task is to complete the function **numProvinces()** which takes an integer V and an adjacency matrix adj as input and returns the number of provinces. adj[i][j] = 1, if nodes i and j are connected and adj[i][j] = 0, if not connected.

**Expected Time Complexity:** O(V^{2})

**Expected Auxiliary Space:** O(V)

**Constraints:**

1 ≤ V ≤ 500

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Number of Provinces

...