Problem · Graph
Maximum Difference
Learn this problemProblem statement
Given a set of nodes and a set of edges between pairs of nodes, first identify the connected components in the graph. A connected component is a group of nodes that are all linked, either directly or through other nodes in the group.
For each connected component, calculate the difference between its largest and smallest node values, and return the maximum of these differences.
Function
maximumDifference(g_nodes: int, g_from: int[], g_to: int[]) → intComplete the function maximumDifference in the editor below.
maximumDifference has the following parameters:
int g_nodes: the number of nodes in the graphint g_from[g_edges]: one endpoint of each edgeint g_to[g_edges]: the other endpoint of each edge
Returns:
int: the maximum difference between the largest and smallest node value within any connected component
Examples
Example 1
g_nodes = 4g_from = [1, 2]g_to = [2, 3]return = 2Nodes 1, 2, and 3 form one connected component, whose difference is 3 - 1 = 2. Node 4 is isolated, so its component difference is 0. The maximum difference is 2.
Example 2
g_nodes = 6g_from = [1, 2, 4]g_to = [2, 3, 6]return = 2The connected components are {1, 2, 3}, {4, 6}, and {5}. Their differences are 2, 2, and 0, so the result is 2.
Example 3
g_nodes = 5g_from = [1, 4]g_to = [5, 2]return = 4The components are {1, 5}, {2, 4}, and {3}. Their differences are 4, 2, and 0, so the maximum difference is 4.
Constraints
1 ≤ g_nodes ≤ 1051 ≤ g_edges ≤ min(105, (g_nodes × (g_nodes - 1)) / 2)
More Akuna Capital problems
- Binary CircuitSeen Jul 2026
- Minimize Malware Spread by Removing a NodeOA · Seen Jul 2026
- Sort Array by FrequencyOA · Seen Jul 2026
- Array Challenge (QR Intern)OA · Seen Jul 2026
- Communications HandlerOA · Seen Jul 2026
- K Smallest SubstringOA · Seen Jul 2026
- Maximum K-Star SumOA · Seen Jul 2026
- Delivery Management SystemOA · Seen Jul 2026