Maximum Label Difference in a Connected Component
Learn this problemProblem statement
You are given an undirected graph with gNodes vertices labeled from 1 through gNodes. For every index i, gFrom[i] and gTo[i] are the endpoints of one undirected edge.
For each connected component, find the difference between its largest and smallest vertex labels. Return the maximum such difference across all connected components. An isolated vertex is a connected component whose difference is 0.
Function
maximumDifference(gNodes: int, gFrom: List<Integer>, gTo: List<Integer>) → intExamples
Example 1
gNodes = 7gFrom = [1,2,3,5,6]gTo = [2,4,5,7,7]return = 4The components are {1, 2, 4} and {3, 5, 6, 7}. Their label differences are 4 - 1 = 3 and 7 - 3 = 4, so the answer is 4.
Example 2
gNodes = 4gFrom = []gTo = []return = 0All four vertices are isolated, so every component has equal minimum and maximum labels and contributes a difference of 0.
Example 3
gNodes = 9gFrom = [1,6,2,3,4]gTo = [6,9,3,4,2]return = 8The component {1, 6, 9} has label difference 9 - 1 = 8. The cycle {2, 3, 4} has difference 2, and all remaining vertices are isolated.
Constraints
gNodesis nonnegative.gFrom.length == gTo.length.- Every endpoint is between
1andgNodes, inclusive. - Parallel edges and self-loops are allowed.
More Akuna Capital problems
- Binary CircuitOA · Seen Jul 2026
- Items SortOA · Seen Jul 2026
- Minimize Malware Spread by Removing a NodeOA · Seen Jul 2026
- Maximum K-Star SumOA · Seen Jul 2026
- Profitable Project PairsOA · Seen Jul 2026
- Array Challenge (QR Intern)OA · Seen Jul 2026
- Communications HandlerOA · Seen Jul 2026
- K Smallest SubstringOA · Seen Jul 2026