FastPrepFastPrep
Problem Brief

Visible Profiles Count

FULLTIMEOA

A popular social media platform provides a feature to connect people online. Connections are represented as an undirected graph where a user can see the profiles of those they are connected to.

There are connection_nodes users numbered 1 to connection_nodes, and connection_edges connections where the pth pair connects nodes connection_from[p] and connection_to[p]. The queries array contains node numbers. Find the number of users whose profiles are visible to query[p]. Report an array of integers where the pth value is the answer to the pth query.

Function Description

Complete the function visibleProfilesCount in the editor.

visibleProfilesCount has the following parameters:

  1. connection_nodes: an integer, the number of users
  2. connection_edges: an integer, the number of connections
  3. connection_from[p]: an array of integers, the starting nodes of each connection
  4. connection_to[p]: an array of integers, the ending nodes of each connection
  5. queries: an array of integers, the node numbers to query

Returns

int[]: an array of integers where the pth value is the number of visible profiles for the pth query

1Example 1

Input
connection_nodes = 7, connection_edges = 4, connection_from = [1, 2, 3, 5], connection_to = [2, 3, 4, 6], queries = [1, 3, 5, 7]
Output
[4, 4, 2, 1]
Explanation
Example 1 illustration

For each query, the number of visible profiles is calculated as follows:

  • Query 1: Visible Profiles are [1, 2, 3, 4]. Number of Visible Profiles is 4.
  • Query 3: Visible Profiles are [1, 2, 3, 4]. Number of Visible Profiles is 4.
  • Query 5: Visible Profiles are [5, 6]. Number of Visible Profiles is 2.
  • Query 7: Visible Profiles are []. Number of Visible Profiles is 1 (the user can see their own profile).

Therefore, the function returns [4, 4, 2, 1].

Constraints

Limits and guarantees your solution can rely on.

🐳
public int[] visibleProfilesCount(int connection_nodes, int connection_edges, int[] connection_from, int[] connection_to, int[] queries) {
  // write your code here
}
Input

connection_nodes

7

connection_edges

4

connection_from

[1, 2, 3, 5]

connection_to

[2, 3, 4, 6]

queries

[1, 3, 5, 7]

Output

[4, 4, 2, 1]

Sign in to submit your solution.