684. Redundant Connection
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find a "redundant" edge in a graph. This graph has n nodes and n edges. It's stated that the graph was originally an n-node tree (which has n-1 edges), and one extra edge was added.
Solution
Redundant Connection in Python
class Solution:
def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
n = len(edges)
# Initialize parent array for DSU (1-based indexing).
# parent[i] = i for i in 1..n
parent = list(range(n + 1))
def find(i):
# Find operation with path compression.
if parent[i] == i:
return i
parent[i] = find(parent[i])
return parent[i]
def union(i, j):
# Union operation.
# Returns True if the sets were merged, False otherwise.
root_i = find(i)
root_j = find(j)
if root_i != root_j:
parent[root_i] = root_j
return True
return False
for u, v in edges:
# If u and v are already connected, this edge is redundant.
if not union(u, v):
return [u, v]
# This part should be unreachable based on problem constraints.
return []
Redundant Connection in Java
class Solution {
private int[] parent;
public int[] findRedundantConnection(int[][] edges) {
int n = edges.length;
parent = new int[n + 1];
// Initialize parent array for DSU (1-based indexing).
for (int i = 1; i <= n; i++) {
parent[i] = i;
}
for (int[] edge : edges) {
int u = edge[0];
int v = edge[1];
// If union returns false, it means u and v were already connected.
// This edge is the redundant one.
if (!union(u, v)) {
return edge;
}
}
// Problem guarantees a solution exists, so this is unreachable.
return new int[0];
}
// Find operation with path compression.
private int find(int i) {
if (parent[i] == i) {
return i;
}
parent[i] = find(parent[i]);
return parent[i];
}
// Union operation. Returns true if union was successful (sets were different),
// false if they were already in the same set.
private boolean union(int i, int j) {
int root_i = find(i);
int root_j = find(j);
if (root_i != root_j) {
parent[root_i] = root_j;
return true;
}
return false;
}
}
Redundant Connection in C++
class Solution {
public:
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
int n = edges.size();
parent.resize(n + 1);
// Initialize parent array for DSU (1-based indexing).
for (int i = 1; i <= n; ++i) {
parent[i] = i;
}
for (const auto& edge : edges) {
int u = edge[0];
int v = edge[1];
// If u and v are already in the same set, this edge is redundant.
if (find(u) == find(v)) {
return edge;
}
// Otherwise, unite the sets of u and v.
unite(u, v);
}
// According to the problem statement, a solution always exists.
// This part should not be reached.
return {};
}
private:
vector<int> parent;
// Find operation with path compression.
int find(int i) {
if (parent[i] == i) {
return i;
}
return parent[i] = find(parent[i]);
}
// Union operation.
void unite(int i, int j) {
int root_i = find(i);
int root_j = find(j);
if (root_i != root_j) {
parent[root_i] = root_j;
}
}
};
Redundant Connection in TypeScript
function findRedundantConnection(edges: number[][]): number[] {
const n = edges.length;
// Initialize parent array for DSU (1-based indexing).
const parent: number[] = new Array(n + 1).fill(0).map((_, i) => i);
// Find operation with path compression.
function find(i: number): number {
if (parent[i] === i) {
return i;
}
parent[i] = find(parent[i]);
return parent[i];
}
// Union operation.
// Returns true if the sets were merged, false otherwise.
function union(i: number, j: number): boolean {
const rootI = find(i);
const rootJ = find(j);
if (rootI !== rootJ) {
parent[rootI] = rootJ;
return true;
}
return false;
}
for (const edge of edges) {
const [u, v] = edge;
// If u and v are already connected, this edge is redundant.
if (!union(u, v)) {
return edge;
}
}
// Unreachable under problem constraints.
return [];
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.