At the Ironhold Duelists' Guild, every one of the n guild members has faced every other member in exactly one sparring duel, and the guild chronicler has logged every outcome in a results ledger. The ledger is given as an n x n grid of 0s and 1s: ledger[i][j] = 1 means duelist i defeated duelist j, and whenever i != j, exactly one of ledger[i][j] and ledger[j][i] equals 1. The guild's duels are always consistent with a strict pecking order: if duelist a defeated duelist b, and duelist b defeated duelist c, then duelist a is guaranteed to have also defeated duelist c. Because of this guarantee, there is always exactly one duelist who defeated every other duelist in the guild — the reigning Guild Champion. Given the ledger, report the 0-indexed id of that duelist.
ledger[i][0..n-1]. It is guaranteed that ledger[i][i] = 0 for every i, and ledger[i][j] + ledger[j][i] = 1 for every i != j.Print a single integer: the 0-indexed id of the duelist who defeated every other duelist.
ledger[i][j] is 0 or 1Example 1
Input
3 0 1 1 0 0 1 0 0 0
Expected
0
Explanation
Duelist 0's row is `0 1 1`, meaning duelist 0 defeated both duelist 1 and duelist 2 — every other duelist. That gives duelist 0 n-1 = 2 wins and 0 losses, so duelist 0 is the champion.
Example 2
Input
4 0 1 0 1 0 0 0 0 1 1 0 1 0 1 0 0
Expected
2
Explanation
Row 2 is `1 1 0 1`, meaning duelist 2 defeated duelists 0, 1, and 3 — every other duelist — so duelist 2 is the champion, even though duelist 0 also has wins (against 1 and 3, but not against 2).
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 →