


If it is impossible to finish all courses, return an empty array. If there are multiple answers, return any one.
Input:
3 2
1 2
2 3
There are three courses to take. To start with, First course 3 is taken. Then course 2 is taken for which course 3 must be completed.
At last course 1 is taken for which course 2 must be completed. So the correct course order is [3,2,1].
The first line contains two integers 'N' and 'M' representing the number of courses and the number of prerequisite pairs.
The next ‘M’ lines in each test case contain a matrix ‘PREREQUISITES’ containing two integers denoting a prerequisite pair.
The only line contains a single integer 1 if the returned order of the courses is correct, otherwise 0.
You don't need to print anything, it has already been taken care of. Just implement the given function.
Our current algorithm is based on the idea of the BFS approach. We first process all the courses with 0 in-degree implying no prerequisite courses required. If we remove all these courses from the graph, along with their outgoing edges, we can find out the courses/nodes that should be processed next. These would again be the nodes with 0 in-degree. We can continuously do this until all the courses have been accounted for.
The steps are as follows:
We need to get all the courses that have a particular course as a prerequisite. If a valid ordering of courses is possible, the course ‘A’ would come before all the other set of courses that have it as a prerequisite. This idea for solving the problem can be explored using a depth-first search.
The steps are as follows: