A chat app wants to show how many separate groups its users form. Users are numbered 0 to n - 1, and pairs[i] = [a, b] means a and b have chatted. Two users are in the same group if you can get from one to the other through a chain of chats.
Write countGroups(n, pairs) that returns the number of separate groups. A user who has never chatted is a group of one.
Input: n = 5, pairs = [[0,1],[1,2],[3,4]] Output: 2
Users 0, 1, and 2 form one group; 3 and 4 form another.
Input: n = 3, pairs = [] Output: 3
Nobody has chatted, so each user is alone.
1 <= n <= 10^50 <= pairs.length <= 2 * 10^50 <= a, b < nRun tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #415 Count Separate Chat Groups. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.