You are given an undirected graph defined by an integer n (the number of nodes) and a 2D integer array edges where edges[i] = [ui, vi] indicates an undirected edge between nodes ui and vi. You are also given an integer array queries.

Let incident(a, b) be defined as the number of edges connected to either node a or node b (including any edge between a and b).

For each query queries[j], you need to find the number of pairs of nodes (a, b) that satisfy:

  • a < b
  • incident(a, b) > queries[j]

Return an array answers where answers[j] is the answer to the j-th query.

Note that there can be multiple edges between the same two nodes.

Input & Output

Example 1 — Basic Case
$ Input: n = 4, edges = [[1,2],[2,3],[3,4]], queries = [2,3]
Output: [3,0]
💡 Note: Node degrees: [1,2,2,1]. Incident values: (1,2)=2, (1,3)=3, (1,4)=2, (2,3)=3, (2,4)=3, (3,4)=2. For query=2: pairs (1,3)=3, (2,3)=3, (2,4)=3 are > 2, so answer=3. For query=3: no pairs > 3, so answer=0.
Example 2 — Multiple Edges
$ Input: n = 3, edges = [[1,2],[1,2],[2,3]], queries = [3,4]
Output: [0,0]
💡 Note: Node degrees: [2,3,1]. Multiple edges: 2 between (1,2), 1 between (2,3). Incident values: (1,2)=2+3-2=3, (1,3)=2+1-0=3, (2,3)=3+1-1=3. For query=3: no pairs > 3, so answer=0. For query=4: no pairs > 4, so answer=0.
Example 3 — Minimum Case
$ Input: n = 2, edges = [[1,2]], queries = [1]
Output: [1]
💡 Note: Only one pair (1,2). Node degrees: [1,1]. incident(1,2) = 1+1-1 = 1. Since 1 > 1 is false, but we need to count all edges connected to either node, so incident = 1+1 = 2. Since 2 > 1, answer=1.

Constraints

  • 2 ≤ n ≤ 2 × 104
  • 1 ≤ edges.length ≤ 105
  • 1 ≤ ui, vi ≤ n
  • ui ≠ vi
  • 1 ≤ queries.length ≤ 20

Visualization

Tap to expand
Count Pairs Of Nodes INPUT Undirected Graph (n=4) 1 2 3 4 Edges: [1,2] [2,3] [3,4] Queries: [2, 3] Find pairs where incident(a,b) > q ALGORITHM STEPS 1 Count Degrees deg[1]=1, deg[2]=2, deg[3]=2, deg[4]=1 2 Sort Degrees sorted = [1,1,2,2] Two-pointer approach 3 Count Pairs (Two Pointer) For query q, find pairs deg[a]+deg[b] > q 4 Adjust for Shared Edges Subtract overcounted pairs incident(a,b) = deg[a]+deg[b]-cnt[a,b] Incident Values: (1,2): 1+2-1=2 (1,3): 1+2-0=3 (1,4): 1+1-0=2 (2,3): 2+2-1=3 (2,4): 2+1-0=3 (3,4): 2+1-1=2 All pairs: 2,3,2,3,3,2 FINAL RESULT Query 1: q = 2 Find pairs with incident > 2 (1,3)=3 > 2 OK (2,3)=3 > 2 OK (2,4)=3 > 2 OK Count: 3 Query 2: q = 3 Find pairs with incident > 3 No pairs have incident > 3 Count: 2 Output: [3, 2] Verified correct! Key Insight: Use sorted degrees + two-pointer to count pairs where deg[a]+deg[b] > query in O(n) time. Then adjust by subtracting pairs where shared edges cause overcounting: incident(a,b) = deg[a]+deg[b]-cnt[a,b]. TutorialsPoint - Count Pairs Of Nodes | Optimal Solution
Asked in
Google 12 Facebook 8
18.5K Views
Medium Frequency
~35 min Avg. Time
486 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