Happy Group
Question Breif
A group of blocks is said to be connected if we can reach from any given block to any other block in the same group, and this group is known as happy group . Given N blocks (numbered from 1 to N) and two lists of size M (u and v) denoting block u[i] is connected to block v[i] and vice versa . Can you count the number of happy groups.
Note: A block is always connected to himselfLink to question:
https://www.hackerrank.com/contests/mindstormer-apr20/challenges/happy-group
Approach:
Graph- BFS
- we keep arrays u and v as given in the question.
- We create a 2D array-edges(ed) which keep record of whichblocks are connected with each other.
- We maintain visited array(vis) that keep record of which block is visited and which is left.
The main thing used is BFS and graph to solve this problem
Comments
Post a Comment