Add Edges to Make Degrees of All Nodes Even - Problem

There is an undirected graph consisting of n nodes numbered from 1 to n. You are given the integer n and a 2D array edges where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi. The graph can be disconnected.

You can add at most two additional edges (possibly none) to this graph so that there are no repeated edges and no self-loops.

Return true if it is possible to make the degree of each node in the graph even, otherwise return false.

The degree of a node is the number of edges connected to it.

Input & Output

Example 1 — Basic Case
$ Input: n = 5, edges = [[1,2],[2,3],[3,4]]
Output: false
💡 Note: Current degrees: node 1=1, node 2=2, node 3=2, node 4=1, node 5=0. Nodes 1, 4, and 5 have odd degrees (3 total). Since we can add at most 2 edges affecting 4 endpoints, we cannot make all 3 odd-degree nodes even.
Example 2 — Already Valid
$ Input: n = 4, edges = [[1,2],[3,4]]
Output: true
💡 Note: All nodes have degree 1 (odd). We have 4 odd nodes, so we can add edges (1,3) and (2,4) to make all degrees even.
Example 3 — Impossible Case
$ Input: n = 4, edges = [[1,2],[1,3],[1,4]]
Output: false
💡 Note: Node 1 has degree 3, nodes 2,3,4 have degree 1. That's 4 odd nodes, but all pairs involve node 1, making it impossible with only 2 edges.

Constraints

  • 1 ≤ n ≤ 105
  • 0 ≤ edges.length ≤ 105
  • edges[i].length == 2
  • 1 ≤ ai, bi ≤ n
  • ai ≠ bi
  • There are no repeated edges

Visualization

Tap to expand
Add Edges to Make Degrees of All Nodes Even INPUT Undirected Graph (n=5) 1 2 3 4 5 Node Degrees: Node 1: degree=1 (odd) Node 2: degree=2 (even) Node 3: degree=2 (even) Node 4: degree=1 (odd) Node 5: degree=0 (even) edges=[[1,2],[2,3],[3,4]] ALGORITHM STEPS 1 Count Odd Degrees Find nodes with odd degree Odd nodes: [1, 4] 2 Check Count Cases 0 odd: already done 2 odd: add 1 edge 4 odd: add 2 edges 3 We have 2 odd nodes Try connecting nodes 1-4 Edge [1,4] doesn't exist 4 Validate Solution Add edge [1,4]: Node 1: 1+1=2 (even) Node 4: 1+1=2 (even) Valid odd counts: 0, 2, or 4 (can fix with 0-2 edges) Other counts: impossible FINAL RESULT Graph After Adding Edge 1 2 3 4 5 NEW All Degrees Now Even: Node 1: 2, Node 2: 2 Node 3: 2, Node 4: 2 Node 5: 0 Output: true OK - 1 edge added Key Insight: Mathematical Graph Theory Each edge contributes 2 to total degree sum (endpoints). Adding an edge changes parity of exactly 2 nodes. With at most 2 edges, we can only fix 0, 2, or 4 odd-degree nodes. For 2 odd nodes: connect them directly, or use an intermediate node. For 4 odd nodes: try pairing them. Any other count is impossible. TutorialsPoint - Add Edges to Make Degrees of All Nodes Even | Mathematical Graph Theory Approach
Asked in
Google 25 Microsoft 18 Amazon 15
28.5K 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