28 · Union-Find & Minimum Spanning Trees

#684 Redundant Connection

Mediumsolve on LeetCode ↗

frequently reported atGoogleAmazonMicrosoft

the problem

In plain words

A tree with n nodes (labelled 1..n) had one extra edge added. Given the n edges, return an edge you can remove so the result is a tree again. If there are several answers, return the one that appears last in the input.

in edges = [[1,2],[1,3],[2,3]]

out [2,3]

in edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]

out [1,4]

  • n == edges.length
  • 3 ≤ n ≤ 1000
  • 1 ≤ aᵢ < bᵢ ≤ n, no repeated edges
  • The graph is connected

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #261
      Graph Valid Tree
      Mediumin Union-Find & MSTLeetCode Premium
    2. #1319
    3. #207
      Course Schedule
      Mediumin Topo Sort