My approach is using Topological Sorting.
But for buidling the Graph, I am using set. This set contains all the unmarked indices. I am connecting a directed edge from pi to pj if pi<pj. I think this way my graph will contain O(n^2) edges and thus it gives me TLE.
I know that my approach is correct, but how can I implement a better way of topological Sort such that it does not take O(n^2) time.
My code for reference: