FastPrepFastPrep
Problem Brief

Number of Good Pairs

OA

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. 1. N: the number of nodes
  2. 2. K: the number of edges
  3. 3. int V[]: an array of integers representing one end of the edge
  4. 4. int U[]: an array of integers representing the other end of the edge

Returns

int: the number of good pairs

1Example 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

Limits and guarantees your solution can rely on.

~~
public int numberOfGoodPairs(int N, int K, int[] V, int[] U) {
  // write your code here
}
Input

N

4

K

2

V

[0, 2]

U

[1, 3]

Output

4

Sign in to submit your solution.