
1. The array might also contain duplicates.
2. It is guaranteed that 'X' and 'Y' are two distinct integers i.e. 'X' is not equal to 'Y'.
The first line contains an integer 'T', denoting the number of test cases.
The first line of each test case contains three space-separated integers 'N', 'X', 'Y', as described in the problem statement.
The second line of each test case contains 'N' space-separated integers, the elements of the array.
For each test case, print the minimum distance between 'X' and 'Y' and if any or both of them('X' and 'Y') are not present in the array then print -1.
Print the output of each test case in a separate line
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
0 <= X, Y <= 10^9
0 <= ARR[i] <= 10^9
Where 'T' denotes the number of test cases, 'N' denotes the number of elements in the array ‘ARR’ respectively, 'X' and 'Y' denotes the integer given in the input, and 'ARR[i]' denotes the 'i-th' element of the array 'ARR'.
Time Limit : 1 sec
Approach: In this approach, we’ll use brute force. As we are supposed to find the minimum distance between the two integers, we’ll do this by using nested loops. The outer loop will be used for selecting the X(our first element) and the inner loop will be used for traversing the array in search for Y(another element) and taking the minimum distance between them. Let us understand how we will implement these steps.
In this efficient approach, we will check only consecutive pairs of X and Y. For every element X or Y, we will check the index of the previous occurrence of X or Y. If the previous occurring element is not similar to the current element, then we update the minimum distance.
But a question still arises, What if an X is preceded by another X and that is preceded by a Y? If this is so then how to get the minimum distance between pairs. But if we observe carefully, it can be seen that every X followed by a Y or vice versa can only be the closest pair (minimum distance) so ignore all other pairs. This will space a lot of our time.
Let us understand how we will implement these steps.