Skip to content

Practice Add Edges to Make Degrees of All Nodes Even

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#2508
LeetCode Problem

Add Edges to Make Degrees of All Nodes Even

Problem Statement

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.

Source: Add Edges to Make Degrees of All Nodes Even on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = 5, edges = [[1,2],[2,3],[3,4],[4,2],[1,4],[2,5]]
Output
true

# Example 2

Input
n = 4, edges = [[1,2],[3,4]]
Output
true

# Example 3

Input
n = 4, edges = [[1,2],[1,3],[1,4]]
Output
false

Constraints

3 <= n <= 10^5
2 <= edges.length <= 10^5
edges[i].length == 2
1 <= ai, bi <= n
ai != bi
There are no repeated edges.

Related LeetCode Problems

Practice more problems similar to Add Edges to Make Degrees of All Nodes Even.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.