Hey everyone, creating this thread to discuss the interview problem - Good Bad Graph.
Practise this interview question on Coding Ninjas Studio (hyperlinked with the following link): Good Bad Graph
Problem of the day
There are ‘N’ cities in ninja Kingdom and Mayer of the city wants to connect all the cities using roads by performing the following operations ‘N - 1’ times. Initially, there are no edges between any cities.
At every operation, you are given two cities, ‘A’ and ‘B’, and you have to connect the city if they are not already connected.
After every input edge, there can be multiple connected components.
Component is good if the number of cities of that component is even; otherwise, it is a bad component.
You need to find the absolute difference between total good and bad components after adding each edge.
The first line contains a single integer ‘T’ denoting the number of test cases. The test cases are as follows.
The first line of each test case contains the value ‘N’, denoting the number of cities.
Following ‘N - 1’ lines contain two space-separated integers, ‘u’ and ‘v’, which we have to connect the city ‘u’ and ‘v’.
Output format :
For each test case, return an array of size ‘N - 1’, where the ‘i’th ( 0 <= i < N - 1 ) value represents the absolute difference between total good and bad components after adding the ‘ith’ edge.
Note :
You don’t need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
2 <= N <= 10^5
Time Limit: 1 sec
2
5
1 2
3 4
2 5
2 3
3
1 2
2 3
1 2 0 1
1 1
For test case 1,
After adding the first edge:
we have only one connected component, i.e. (1, 2).
Total good component = 1 ( 1, 2 ).
Total bad component = 0.
After adding the second edge:
we have two connected components, i.e., (1, 2) and (3, 4).
Total good component = 2 ( ( 1, 2 ) , ( 3, 4 ) ).
Total bad component = 0.
After adding the third edge:
we have two connected components, i.e., (1, 2, 5) and (3, 4).
Total good component = 1 ( 3, 4 ).
Total bad component = 1 ( 1, 2, 5 ).
After adding forth edge:
we have only one connected component, i.e., (1, 2, 3, 4, 5).
Total good component = 0.
Total bad component = 1 ( 1, 2, 3, 4, 5 ).
For test case 2,
After adding the first edge:
we have only one connected component, i.e. (1, 2).
Total good component = 1 ( 1, 2 ).
Total bad component = 0.
After adding the second edge:
we have only one connected component, i.e. (1, 2, 3).
Total good component = 0.
Total bad component = 1 ( 1, 2, 3 ).
2
5
1 2
1 3
1 4
1 5
2
1 2
1 1 1 1
1
Try to check for all operations independently
APPROACH :
ALGORITHM :
O( N * N ), where ‘N’ is the total number of cities.
After every operation, we will check the entire graph to find the connected component the time to find all the connected components is O(N), and there are total ‘N’ operations, so the overall time complexity is O( N * N ).
O( N ).
We are storing all input edges and the stack space used is of order O(N) , so the overall space complexity is O( N ).
Interview problems
Discussion thread on Interview Problem | Good Bad Graph
Hey everyone, creating this thread to discuss the interview problem - Good Bad Graph.
Practise this interview question on Coding Ninjas Studio (hyperlinked with the following link): Good Bad Graph