n items, numbered 0 to n - 1, producing the rankings first and second (each is a permutation of 0 to n - 1, best item first).
A triple of distinct items {x, y, z} is **agreed on** if both rankings put them in the same order: for example, x before y and y before z in first and in second alike.
Return the number of agreed triples. Each set of three items is counted once.
Trying every triple is O(n³), and n can be up to 10⁵.first=[1, 0, 2, 3], second=[0, 1, 2, 3] → 2
The triples (1, 2, 3) and (0, 2, 3) come in the same order in both rankings. Any triple holding both 0 and 1 fails, since those two are swapped.
first=[0, 1, 2], second=[0, 1, 2] → 1
The rankings are identical, so the only triple qualifies.
first=[3, 2, 1, 0], second=[0, 1, 2, 3] → 0
The second ranking is the first one reversed, so no triple keeps its order.
first and second are permutations of 0 to n - 1first=[1, 0, 2, 3], second=[0, 1, 2, 3]
2
Run checks the first 3 tests in your browser. Submit checks all 103 tests on the server, hidden ones included.