TikTok: Secondary Influencers
Learn this problemProblem statement
TikTok's influencer network can be represented as a tree of g_nodes numbered from 1 to g_nodes. Each connection between influencers is represented by an edge in the tree, where the i-th edge connects the influencers g_from[i] and g_to[i].
Suppose the maximum distance, i.e., the number of connections between any two influencers, is mx. An influencer is considered primary if they lie on the simple path between two influencers u and v such that the distance between u and v is equal to mx. All other influencers are secondary.
Your task is to find the sum of indices of all the secondary influencers.
Function
getSecondaryInfluencerSum(g_nodes: int, g_from: int[], g_to: int[]) → long
Complete the function getSecondaryInfluencerSum in the editor.
getSecondaryInfluencerSum has the following parameters:
int g_nodes: the number of influencers in the network.int g_from[g_edges]: one influencer in each connection.int g_to[g_edges]: the other influencer in each connection.
Returns
long int: the sum of indices of the secondary influencers.
Examples
Example 1
g_nodes = 4g_from = [1, 1, 2]g_to = [2, 3, 4]return = 0
Example 2
g_nodes = 6g_from = [1, 1, 1, 2, 3]g_to = [2, 3, 4, 5, 6]return = 4
Example 3
g_nodes = 5g_from = [1, 1, 2, 2]g_to = [2, 3, 4, 5]return = 0
Constraints
More Tiktok problems
- Count Access Code PairsOA · Seen Jul 2026
- Count Key ChangesOA · Seen Jul 2026
- Travel Distance on ScootersOA · Seen Jul 2026
- Count Skipped Numbers After SubtractionsOA · Seen Jul 2026
- Obstacle Placement QueriesOA · Seen Jul 2026
- Repeated Grouped Digit SumOA · Seen Jul 2026
- Count Cyclic Digit PairsOA · Seen Jun 2026
- Event ID Check Completion TimesOA · Seen Jun 2026