Description
Solutions
Number of Good Pairs
📚RELATED PROBLEMS
There is a graph and the number of Nodes on that graph is N
and also given K
edges, now you need to find the number of good pairs in that graph,
The definition of the good pairs in that graph is:
- On those pairs, two nodes not present in the same components of that graph, means both the nodes present in different connected components and there are no connected paths in between them.
Now, you are given N
the number of nodes and K
the number of edges and the connected edges in the form of array V
and array U
.
Function Description
Complete the function numberOfGoodPairs
in the editor.
numberOfGoodPairs
has the following parameters:
- 1.
N
: the number of nodes - 2.
K
: the number of edges - 3.
int V[]
: an array of integers representing one end of the edge - 4.
int U[]
: an array of integers representing the other end of the edge
Returns
int: the number of good pairs
Example 1:
Input: N = 4, K = 2, V = [0, 2], U = [1, 3]
Output: 4
Explanation:There are 4 nodes and 2 edges. The edges are between nodes 0-1 and 2-3. This results in two separate connected components: {0, 1} and {2, 3}. The good pairs are those where each node is from a different component. The good pairs are: 0-2, 0-3, 1-2, 1-3. Therefore, there are 4 good pairs.
Constraints:
~~

Related Problems
Testcase
Result
Case 1
input:
output: