See the original problem on HackerRank.
Given two arrays, A and B, of size N that both contain permutations of the same set of unique integers, find and print the number having the absolute minimum index difference between the two arrays. In the event of a tie, choose the smallest number.
Input Format
The first line contains an integer, N, denoting the size of the array. The second line contains N spaceseparated integers describing array A (a0, a1, …). The third line contains N spaceseparated integers describing array B (b0, b1, …).
Contraints


Output Format
Print the number having the absolute minimum index difference between the two arrays. In the event of a tie, choose the smallest number.
Example
Input:


Output:


Explanation:
The absolute difference between the indices for each number are as follows:


As you can see, the minimum index difference is 2 and there are two numbers, 8 and 9, having this minimum index difference. Since 8 is smaller, we print 8.
Solutions
We can store distances into a sorted map. First, we set distance of element \(i\) as the index of \(i\) in the first array  it’s like a baseline.
Afterwards, we read the second array and update the distances by subtracting the index in the map with the current index in the second array.
Finally, we calculate the minimum value. Since the map is sorted, the first minimum retrieved is also the smallest one.
Here is an idea in C++:

