Given two permutations of (1, 2, 3,..N),
Consider for n = 5
5 4 3 2 1
3 2 4 1 5
Find number of pairs (a,b) such that index(a)<index(b) in both of the permutations?
In the above case, answer is 4.
(4,1) index(4)<index(1) in both permutations.
(3,1)
(2,1)
(3,2)
We can easily do this in O(n2) but I feel like we can do this in O(nlogn). Can you please help?
Another example...
for n = 5
3 4 1 5 2
1 3 2 5 4
Here answer is 5
(3,4) index(3)<index(4) in both perms.
(3,5) index(3)<index(5) in both perms.
(3,2)
(1,5)
(1,2)