Any better alternative to do CHEFTRAV

I did this question using link list.But it showed me TLE for both the test cases.

link to ans:

Any other better approach?


This Problem can be reduced to a basic graph problem where we are given source and destination and we have to go from source to destination.

finding source: the source[i] which is occuring only once

finding destination: the dest[i] which is occuring only once

void dfs(string source,string destination)
		cout<<source<<"-"<<graph[start]<<" ";

here graph stores destination of the particular source.

hope that helps… complexity is O(n)

1 Like