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

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

<p>Return <code>true</code><em> if it is possible to make the degree of each node in the graph even, otherwise return </em><code>false</code><em>.</em></p>

<p>The degree of a node is the number of edges connected to it.</p>

<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/26/agraphdrawio.png" style="width: 500px; height: 190px;" />
<pre>
<strong>Input:</strong> n = 5, edges = [[1,2],[2,3],[3,4],[4,2],[1,4],[2,5]]
<strong>Output:</strong> true
<strong>Explanation:</strong> The above diagram shows a valid way of adding an edge.
Every node in the resulting graph is connected to an even number of edges.
</pre>

<p><strong>Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/26/aagraphdrawio.png" style="width: 400px; height: 120px;" />
<pre>
<strong>Input:</strong> n = 4, edges = [[1,2],[3,4]]
<strong>Output:</strong> true
<strong>Explanation:</strong> The above diagram shows a valid way of adding two edges.</pre>

<p><strong>Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/26/aaagraphdrawio.png" style="width: 150px; height: 158px;" />
<pre>
<strong>Input:</strong> n = 4, edges = [[1,2],[1,3],[1,4]]
<strong>Output:</strong> false
<strong>Explanation:</strong> It is not possible to obtain a valid graph with adding at most 2 edges.</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>2 &lt;= edges.length &lt;= 10<sup>5</sup></code></li>
	<li><code>edges[i].length == 2</code></li>
	<li><code>1 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt;= n</code></li>
	<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
	<li>There are no repeated edges.</li>
</ul>

from collections import defaultdict

class Solution:
    def isPossible(self, n: int, edges: List[List[int]]) -> bool:
        # Create an adjacency list to represent the graph
        graph = defaultdict(set)
        for u, v in edges:
            graph[u].add(v)
            graph[v].add(u)
        
        # Find nodes with odd degrees
        odd_degree_nodes = [node for node in range(1, n+1) if len(graph[node]) % 2 == 1]
        
        # If there are more than 4 nodes with odd degrees, it's impossible
        if len(odd_degree_nodes) > 4:
            return False
        
        # If there are no odd degree nodes, it's already possible
        if not odd_degree_nodes:
            return True
        
        # Case with 2 odd degree nodes
        if len(odd_degree_nodes) == 2:
            u, v = odd_degree_nodes
            # Check if adding an edge between these two nodes is possible
            if v not in graph[u]:
                return True
            # Check if there exists a common neighbor between u and v
            for i in range(1, n+1):
                if i not in graph[u] and i not in graph[v]:
                    return True
            return False
        
        # Case with 4 odd degree nodes
        if len(odd_degree_nodes) == 4:
            u, v, x, y = odd_degree_nodes
            # Check if any combination of edges can make all degrees even
            for i in range(4):
                for j in range(i+1, 4):
                    a, b = odd_degree_nodes[i], odd_degree_nodes[j]
                    if b not in graph[a]:
                        for k in range(4):
                            if k != i and k != j:
                                c = odd_degree_nodes[k]
                                for l in range(k+1, 4):
                                    if l != i and l != j:
                                        d = odd_degree_nodes[l]
                                        if d not in graph[c]:
                                            return True
            return False
        
        # Case with 1 or 3 odd degree nodes is impossible
        return False

# Example usage:
solution = Solution()
n = 5
edges = [[1,2],[2,3],[3,4],[4,2],[1,4],[2,5]]
print(solution.isPossible(n, edges))  # Output: True
71665