From e92eb158311e728b62d11115d261f328debc7927 Mon Sep 17 00:00:00 2001 From: Sainik Khaddar <73096052+playboySeductor@users.noreply.github.com> Date: Sun, 2 Jul 2023 17:02:28 +0530 Subject: [PATCH] Create README - LeetHub --- Number of Provinces - GFG/README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Number of Provinces - GFG/README.md diff --git a/Number of Provinces - GFG/README.md b/Number of Provinces - GFG/README.md new file mode 100644 index 0000000..ea1d4cd --- /dev/null +++ b/Number of Provinces - GFG/README.md @@ -0,0 +1,29 @@ +# Number of Provinces +## Medium +
Given an undirected graph with V vertices. We say two vertices u and v belong to a single province if there is a path from u to v or v to u. Your task is to 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: +[ + [1, 0, 1], + [0, 1, 0], + [1, 0, 1] +] +++Output: +2 +Explanation: +The graph clearly has 2 Provinces [1,3] and [2]. As city 1 and city 3 has a path between them they belong to a single province. City 2 has no path to city 1 or city 3 hence it belongs to another province. +
Input: +[ + [1, 1], + [1, 1] +] +++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(V2)
Expected Auxiliary Space: O(V)
Constraints:
1 ≤ V ≤ 500