In the realm of Valdris, n heirs, numbered 0 to n-1, have historically pressed claims against one another over the throne. Each recorded claim (a, b) means heir a has an acknowledged stronger claim to the throne than heir b — heir b's ambitions are considered subordinate to heir a's. The royal archive guarantees that no two heirs ever claim superiority over each other in both directions, and no heir is recorded as claiming over themselves. Peace returns to the realm only when a single heir can be recognized as the rightful successor: an heir who is never named as the subordinate side of any recorded claim. Given the full archive of claims, report the rightful successor if the archive identifies exactly one heir meeting this description; otherwise, report that succession remains disputed.
Example 1
Input
5 4 1 0 1 2 1 3 1 4
Expected
1
Explanation
Heir 1 asserts a stronger claim over heirs 0, 2, 3, and 4, and no claim ever names heir 1 as the subordinate side. Since heir 1 never appears as the second value of any claim, heir 1 is the unique rightful successor, so the output is 1.
Example 2
Input
3 2 1 0 2 0
Expected
-1
Explanation
Heirs 1 and 2 both assert claims over heir 0, but neither heir 1 nor heir 2 is ever named as the subordinate side of any claim. Two heirs (1 and 2) qualify as unchallenged, so succession remains disputed and the output is -1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →