Largest Study Group - Problem

In a university, there are n students numbered 0 to n - 1. Some students are friends.

You are given a 2D array friendships where friendships[i] = [u, v] means students u and v are friends. Friendship is transitive.

Return the size of the largest study group.

Input & Output

Example 1
$ Input: n=7, friendships=[[0,1],[0,2],[1,3],[4,5],[5,6]]
Output: 4
💡 Note: Group {0,1,2,3}=4, Group {4,5,6}=3. Largest=4.
Example 2
$ Input: n=4, friendships=[[0,1],[1,2],[2,3]]
Output: 4
💡 Note: All connected.
Example 3
$ Input: n=5, friendships=[]
Output: 1
💡 Note: Each alone.

Constraints

  • 1 ≤ n ≤ 2×10^5
  • 0 ≤ friendships.length ≤ 2×10^5
  • 0 ≤ u, v < n

Visualization

Tap to expand
Largest Study Group INPUT Students: n = 7 Friendship Graph: Group A 0 1 2 3 Group B 4 5 6 Find the size of the largest study group. ALGORITHM STEPS 1 Build Friendship Graph Adjacency list from pairs 2 BFS Each Component Start from each unvisited node 3 Count Component Size Count nodes in each BFS 4 Return Maximum Track and return largest size Execution Trace: BFS# start members size 1 0 {0,1,2,3} 4 2 4 {4,5,6} 3 max(4, 3) = 4 FINAL RESULT Study Groups Found: 📚 Group A — Size: 4 👩‍🎓👨‍🎓👩‍🎓👨‍🎓 Students: 0, 1, 2, 3 📚 Group B — Size: 3 👩‍🎓👨‍🎓👩‍🎓 Students: 4, 5, 6 Largest group has 4 members! Output: 4 💡 Key Insight: The largest study group equals the largest connected component in the friendship graph. BFS finds each component in O(V+E). Union-Find with size tracking returns max directly. TutorialsPoint - Largest Study Group | BFS Component Counting Approach
Asked in
Amazon 0 Meta 0
2 Views
High Frequency
~12 min Avg. Time
0 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen