Back to problems

Count Connected Components (Union-Find)

Algorithm · Amazon · Medium

Requirements You receive a collection of items together with pairwise declarations that two items belong together, such as products assigned to a shared category or edges in a graph. Treat this relationship as transitive. Determine how many separate groups, or connected components, remain. Certain versions also require the size of every group. Another formulation asks whether inserting a particular edge creates a cycle. Examples Product-group version: Input: ["tea",…

Checking your access…