Please explain what to do in the question.
I do not understand the question
When we analyse the question, we are certain to use topological sort. The way we came to this conclusion of using a graph is we see a dependency of 1 element on the other, basically, if we analyze the output for the given input we are trying to find the next greater element, marking the present element when pj > pi ( a thought of using a dfs strikes our mind)
We observe that the nodes (numbers) are related to one another, in a way a number will be marked observed if it is greater than the present number. Use graph when u see dependency of task on one another.
Just a suggestion from my experience:
You might think of using a graph where the series is from 1 to n like we generally denote vertices in sequential order. Given that u have thoroughly thought of using a array and u are failing to build some LOGIC.
SO basically dependency of items, connected components, ordering based on some relationship can get you to think of using a GRAPH.