Minimum Degree of a Connected Trio in a Graph - Problem

You are given an undirected graph. You are given an integer n which is the number of nodes in the graph and an array edges, where each edges[i] = [ui, vi] indicates that there is an undirected edge between ui and vi.

A connected trio is a set of three nodes where there is an edge between every pair of them.

The degree of a connected trio is the number of edges where one endpoint is in the trio, and the other is not.

Return the minimum degree of a connected trio in the graph, or -1 if the graph has no connected trios.

Input & Output

Example 1 — Basic Connected Trio
$ Input: n = 6, edges = [[1,2],[1,3],[3,2],[4,1],[5,2],[3,6]]
Output: 3
💡 Note: There are exactly 3 trios: (1,2,3), (1,2,4), and (2,3,5). The trio (1,2,3) has degree 3 (node 1 connects to 4, node 2 connects to 5, node 3 connects to 6), which is minimum.
Example 2 — No Connected Trios
$ Input: n = 7, edges = [[1,3],[4,1],[4,3],[2,5],[5,6],[6,7]]
Output: -1
💡 Note: No set of 3 nodes forms a complete triangle where every pair is connected.
Example 3 — Perfect Triangle
$ Input: n = 3, edges = [[1,2],[2,3],[1,3]]
Output: 0
💡 Note: The only trio is (1,2,3) and all three nodes only connect to each other, so external degree is 0.

Constraints

  • 2 ≤ n ≤ 400
  • edges.length ≤ n * (n-1) / 2
  • edges[i].length == 2
  • 1 ≤ ui, vi ≤ n
  • ui != vi
  • There are no repeated edges

Visualization

Tap to expand
Connected Trio: Find Minimum External Degree123456Trio (1,2,3)Internal edges: 1-2, 1-3, 2-3 (count = 3)External edges: 1-4, 2-5, 3-6 (count = 3)Trio degree = 3 external edgesMinimum Degree: 3
Understanding the Visualization
1
Input Graph
Graph with n=6 nodes and given edges
2
Find Trios
Identify all connected triangles
3
Calculate Degrees
Count external edges for each trio
Key Takeaway
🎯 Key Insight: A trio's degree = sum of node degrees - 6 (internal triangle edges)
Asked in
Facebook 35 Google 28 Amazon 22
33.0K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen