Friend Circle Queries

  • + 0 comments

    This problem is solved using a data structure called a Disjoint Set. Geeks for geeks has a pretty good article on them here: https://www.geeksforgeeks.org/disjoint-set-data-structures-java-implementation/

    Basically you just build separate trees for each "disjoint set" and whenever you need to merge two of them, you make the root of the smaller set a child of the root of the larger set.

    Hackerrank has a few other problems setup to get you used to using this data structure. If you're interested, you can find them here: https://www.hackerrank.com/domains/data-structures?filters%5Bsubdomains%5D%5B%5D=disjoint-set