<script>
function
maximumMatchingPairs(perm1, perm2, n)
{
var
left = Array(n);
var
right = Array(n);
var
mp1 =
new
Map(), mp2 =
new
Map();
for
(
var
i = 0; i < n; i++) {
mp1.set(perm1[i], i);
}
for
(
var
j = 0; j < n; j++) {
mp2.set(perm2[j], j);
}
for
(
var
i = 0; i < n; i++) {
var
idx2 = mp2.get(perm1[i]);
var
idx1 = i;
if
(idx1 == idx2) {
left[i] = 0;
right[i] = 0;
}
else
if
(idx1 < idx2) {
left[i] = (n - (idx2 - idx1));
right[i] = (idx2 - idx1);
}
else
{
left[i] = (idx1 - idx2);
right[i] = (n - (idx1 - idx2));
}
}
var
freq1 =
new
Map(), freq2 =
new
Map();
for
(
var
i = 0; i < n; i++) {
if
(freq1.has(left[i]))
freq1.set(left[i], freq1.get(left[i])+1)
else
freq1.set(left[i], 1)
if
(freq2.has(right[i]))
freq2.set(right[i], freq2.get(right[i])+1)
else
freq2.set(right[i], 1)
}
var
ans = 0;
for
(
var
i = 0; i < n; i++) {
ans = Math.max(ans, Math.max(freq1.get(left[i]),
freq2.get(right[i])));
}
return
ans;
}
var
P1 = [5, 4, 3, 2, 1];
var
P2 = [1, 2, 3, 4, 5];
var
n = P1.length;
document.write( maximumMatchingPairs(P1, P2, n));
</script>