Skip to main content

Happy Group - Mindstormer April 20

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 himself

Link 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.
Further explanation is in the comments with the code.

The main thing used is BFS and graph to solve this problem

#include<iostream>
#include<vector>
#include<queue>
using namespace std;
// for bfs we have to use queue data structure and we are marking all the blocks as visted that can be visited from the starting node
void helperbfs(int **ed,int * vis,int s,int V){// (edge array,visited array,starting node,no. of vertex/blocks)
vector<int> v;
queue<int> q;
q.push(s);
v.push_back(s);
while(!q.size()==0){
int r=q.front();
q.pop();
v.push_back(r);
vis[r]=1;
for(int j=0;j<V;j++){
if(r==j){
continue;
}
if(ed[r][j]==1){
if(vis[j]==1){
continue;
}else{
vis[j]=1;
q.push(j);
}
}
}
}
}
// in bfs we are creating a visited array which keep mark of which node is included in some group or not.
int bfs(int **ed,int n){
int *vis=new int[n];
for(int i=0;i<n;i++){
vis[i]=0;
}
int count=0;
for(int i=0;i<n;i++){
if(!vis[i]){
helperbfs(ed,vis,i,n);// print function is actually performing th function of bfs helperbfs(edge array,visited aray,block no.,no. of blocks)
count++;// every time print function is called it states that there is a new group so we increment count.
}
}
return count;// we return the no. of groups
}
//In solve method we are just creating the edge array we are marking 1 where the edge exist between two blocks.
// And then we are calling the bfs(breadth first search approach for which we a function above)
int solve(int n,int m,vector<int>u,vector<int>v)
{
int **ed=new int*[n];// creating edge array
for(int i=0;i<n;i++){
ed[i]=new int[n];
for(int j=0;j<n;j++){
ed[i][j]=0;
}
}
for(int i=0;i<m;i++){
ed[u[i]-1][v[i]-1]=1;
ed[v[i]-1][u[i]-1]=1;
}
return bfs(ed,n); // calling dfs function bfs(edge array, no. of nodes) it will return the no. of groups
}
// Here we are just getting the input from user as per the question requirement
int main()
{
int n,m;
vector<int>u,v;
cin>>n>>m;
for(int i=0;i<m;i++)
{
int x;
cin>>x;
u.push_back(x);
}
for(int i=0;i<m;i++)
{
int x;
cin>>x;
v.push_back(x);
}
cout<<solve(n,m,u,v)<<endl; // Solve method called
}
view raw HappyGroup.cpp hosted with ❤ by GitHub

Comments

Popular posts from this blog

Brilliant Mind - Mindstormer April 20 (Product Of All Perfect Square Term Upto A Given No 'n')

Product Of All Perfect Square Term Upto n Question Brief   :           A Number 'n' is given to you , you are said to find the product of all the perfect square term less than or equal to n. Prequisite   :   Basic Mathematics, Preprocessing Approach   :      Perfect Square numbers upto any number n are only these:          (1 2 ,2 2 ,........,(√n) 2 )      In Question it was asked to find the product of all perfect square numbers.i.e. ans =  (1 2  x 2 2   x....... x (√n) 2 ) it can be easily transformed to: ans =  (1   x 2   x....... x √n ) 2 ans= (√n!) 2 So, in each query , for given value of any n, we had to print the square of factorial of square root of n. This can be easily answered in O(1) if we already preprocess the number 1 to sqrt(N) and store their factorial already. SETTER CODE  :

Wonder String-1 - Mindstormer April 20

Wonder String Question Breif Given a string containing just the characters 'a', 'b', 'c', 'd', 'e' and 'f', determine if the input string is wonder or not. Pairs are known as (opening element,closing element) : (a,b), (c,d), (e,f) An input string is wonder if: First(opening element) element of pair must be closed by the second(closing element) element of the pair Opening element must be closed by respective closing element in the correct order Note that an empty string is also considered wonder string: Link To Question: https://www.hackerrank.com/contests/mindstormer-apr20/challenges/wonder-string-1 Approach: Hint:  Use Stack If an opening element occurs we push it into stack and when the closing element occurs, we check if the element at the top of stack is corresponding opening element . If its some other element then we return false. Else we pop out  the top most element. If at the end of s...

Makarov's Ambition - Mindstormer April 20

 Makarov's Ambition - Mindstormer April 20  Question Brief   :            Two numbers s (number of states) and d (number of districts) followed by a series of 'n' numbers (0 and 1) will be given to you such that s*d=n. All you have to do is check that is there any way to divide the series into n parts such that in majority of states, the guild is victorious. The guild wins if more than half of the districts are won by the guild. Also the division will be consecutive. For example: if s=3, d=2 and series is a1, a2, a3, a4, a5, a6. so division can be made in only these two ways : (a1, a2), (a3, a4), (a5, a6) (a2, a3), (a4, a5), (a6,a1) Prequisite   :   Basic Programming Approach   :         We have to take two arrays, one to store the series and other stores the sum of series till that index. There can be only that number of ways to divide the states as many district...