You are given a list of pairs, where each pair represents a course dependency in the format [prerequisite, course]. This means the prerequisite must be taken before the course. The input is guaranteed to form a single, unbranched, continuous chain of courses. The total number of courses in this chain is always odd, so there is exactly one middle course. Every course appears at most once as a prerequisite (except the very first course in the chain) and at most once as a subsequent course (except the very last course).
Your task is to find and return the middle course of this chain.
Constraints:
Example 1:
pairs = [ ["Chemistry", "Biology"], ["Biology", "ComputerScience"], ["Math", "Physics"], ["Physics", "Chemistry"] ]
"Chemistry"
The chain is reconstructed as: Math -> Physics -> Chemistry -> Biology -> ComputerScience. With 5 courses in total, the middle course is the third one, "Chemistry".
Example 2:
pairs = [ ["Intro", "Data"], ["Data", "Algo"], ["Algo", "Systems"], ["Systems", "AI"] ]
"Algo"
Example 3:
pairs = [["Math", "Physics"], ["Physics", "Chemistry"]]
"Physics"
Example 1
Input: [["Chemistry","Biology"],["Biology","ComputerScience"],["Math","Physics"],["Physics","Chemistry"]]
Output: "Chemistry"
Example 2
Input: [["Intro","Data"],["Data","Algo"],["Algo","Systems"],["Systems","AI"]]
Output: "Algo"