New update is available. Click here to update.
Close
Topic list
Course Schedule II
HARD
50 mins
12 upvotes
Graph
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Graph
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

# Course Schedule II

Contributed by
Deep Mavani
Hard
0/120
Avg time to solve 50 mins
Success Rate 50 %
Share
12 upvotes

## Problem Statement

#### Your task is to return the ordering of courses you should take to finish all courses.

##### Note:
``````If it is impossible to finish all courses, return an empty array. If there are multiple answers, return any one.
``````
Detailed explanation ( Input/output format, Notes, Constraints, Images )
##### Sample Input 1:
``````2
4
4
2 1
3 1
4 2
4 3
3
2
1 2
2 3
``````
##### Sample Output 1:
``````1
1
``````
##### Explanation of Sample Output 1:
``````In test case 1, There are a total of 4 courses to take. To take course 4 you should have finished both courses 2 and 3. Both courses 2 and 3 should be taken after you finish course 1. So one correct course order is [1,2,3,4]. Another correct ordering is [1,3,2,4]. When the ordering is one of the above two sets then output is 1.

In test case 2, There are 3 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 must be completed. So correct course order is [3,2,1].
``````
##### Sample Input 2:
``````2
2
1
2 1
1
0
``````
##### Sample Output 2:
``````1
1
``````
##### Explanation of Sample Output 2:
``````In test case 1, there are a total of 2 courses to take. To take course 2 you should have finished course 1. So the correct course order is [1, 2]. Ordering [1, 2] is correct.

In test case 2, there is only one course and so no pairs of courses are possible and thus the only correct order of courses is [1].
``````
Auto
Console