VOOZH about

URL: https://www.geeksforgeeks.org/dsa/help-geek-to-avoid-explosion/

⇱ Help geek to Avoid explosion - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Help geek to Avoid explosion

Last Updated : 23 Jul, 2025

Geek is a chemical scientist who is performing an experiment to find an antidote to a poison. The experiment involves mixing some solutions in a flask. Based on the theoretical research Geek has done, he came up with an n * 2 array 'mix', where mix[i] = {X, Y} denotes solutions X and Y that needs to be mixed.

Also, from his past experience, it has been known that mixing some solutions leads to an explosion and thereby completely ruining the experiment. The explosive solutions are also provided as an m * 2 array 'danger' where danger[i] = {P, Q} denotes that if somehow solutions P and Q get into the same flask it will result in an explosion.

Help the Geek by returning an array 'answer' of size n, where answer[i] = "Yes" if it is safe to mix solutions in 'mix[i]' or else answer[i] = "No".

Note: Geek should follow the order of mixing of solutions as it is in 'mix' otherwise the antidote will be ineffective. Also, Geek will not mix the solutions in 'mix[i]' once he gets to know that mixing them will result in an explosion. 

Examples:

Input: n = 5, m = 2
mix = {{1, 2}, {2, 3}, {4, 5}, {3, 5}, {2, 4}}
danger = {{1, 3}, {4, 2}}
Output: {"Yes", "No", "Yes", "Yes", "No"}
Explanation: Mixing the first solution(1 and 2) of 'mix' do not result in any kind of explosion hence answer[0] is "Yes", while mixing 2 and 3 is not allowed because it will result in an explosion as 1 and 3 would be in same solution hence we have returned "No" as the answer for it. Mixing the third solution(4 and 5) and 4th solution(3 and 5) of 'mix' do not result in any kind of explosion hence answer[2] and answer[3] is "Yes". While mixing 2 and 4 is not allowed because it will result in an explosion hence we have returned "No" as the answer for it.

Input: n = 3, m = 2
mix = {{1, 2}, {2, 3}, {1, 3}}
danger = {{1, 2}, {1, 3}}
Output: {"No", "Yes", "No"}
Explanation: Mixing solutions 1 and 2 is dangerous hence answer[0] = 7"No", but solutions 2 and 3 can be mixed without any problem therefore answer[1] = "Yes". Again, mixing solutions 1 and 3 is dangerous due to which answer[2] = "No".

Approach: To solve the problem follow the below idea:

Geek should blend solutions in the same sequence as in the mixed array, thus we need to build a data structure that can group all the elements together that have already been mixed. Disjoint set data structure can verify the presence of two elements in log(n) time, thus we can utilize it.

Below are the steps for the above method:

  • First, each element must be made the parent of itself by being stored in the parent array.
  • We must determine whether there will be an explosion once each pair of the mix arrays has been combined. The representation of each element can be compared to do this process.
  • Use the union function to combine them if there is no risk of explosion. and save the response in a vector of string say res.

Below is the code for the above approach:


Output
[Yes, No, Yes, Yes, No]

Time Complexity: O(n*m*log(n))
Auxiliary Space: O(n)

Comment
Article Tags:
Article Tags: