🚀🚀684. Redundant Connection🔥🔥|| ✅Efficient Solution by Kanishka✅|| 🔥🔥Beats 75% in Java🚀🚀
🧠IntuitionWe need to find an edge that, when removed, allows all nodes to be connected without any cycles. This means we need to detect a cycle in the graph.🔍 A
# 🧠Intuition
We need to find an edge that, when removed, allows all nodes to be connected without any cycles. This means we need to detect a cycle in the graph.

# 🔍 Approach
We'll use the Union-Find data structure to help detect cycles. As we add each edge, we'll use the Union-Find operations to check if adding the edge would form a cycle. If it does, then that's the redundant connection.

# ⏳Complexity
- Time complexity:
  $$O(E \cdot \alpha(V))$$, where $$E$$ is the number of edges and $$\alpha$$ is the inverse Ackermann function. This is almost constant time.

- Space complexity:
  $$O(V)$$, where $$V$$ is the number of nodes.

# 📜Code
```java []
class Solution {
    public int[] findRedundantConnection(int[][] edges) {
        UnionFind uf = new UnionFind(edges.length);
        for (int[] edge : edges) {
            int first = uf.find(edge[0] - 1);
            int second = uf.find(edge[1] - 1);

            if (first == second) {
                return new int[] {edge[0], edge[1]};
            } else {
                uf.union(first, second);
            }
        }
        return null;
    }

    static class UnionFind {
        private final int[] parent;
        private final int[] rank;

        public UnionFind(int size) {
            parent = new int[size];
            rank = new int[size];
            for (int i = 0; i < size; i++) {
                parent[i] = i;
                rank[i] = 1;
            }
        }

        public int find(int x) {
            if (parent[x] != x) {
                parent[x] = find(parent[x]);
            }
            return parent[x];
        }

        public void union(int x, int y) {
            int rootX = find(x);
            int rootY = find(y);
            if (rootX != rootY) {
                if (rank[rootX] > rank[rootY]) {
                    parent[rootY] = rootX;
                } else if (rank[rootX] < rank[rootY]) {
                    parent[rootX] = rootY;
                } else {
                    parent[rootY] = rootX;
                    rank[rootX]++;
                }
            }
        }
    }
}
```

##### I hope this helps! Feel free to reach out if you need more assistance. Happy coding! 🚀✨
['Java']