Solving HackerRank Problem Journey to the Moon in java and python 3.


The member states of the UN are planning to send  2 people to the moon. They want them to be from different countries. You will be given a list of pairs of astronaut ID's. Each pair is made of astronauts from the same country. Determine how many pairs of astronauts from different countries they can choose from.

See full description of problem Journey to the Moon

Python 3 Solution

Using Quick Find approach to build  Connected components. Basically each Connected  Component is a Country. Number of nodes in each component is number of people in each country.  Using a counts array and an iteration over connected components array to get count of people in each country. Simple Mathematics to get total possibilities.

Time Complexity: O(N + I)

Space Complexity: O(N)