Map to store index of elements
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 ++ ) {