#K12266. Maximum Distance Pair of Equal Elements
Maximum Distance Pair of Equal Elements
Maximum Distance Pair of Equal Elements
Given an array of n integers, your task is to find two indices \( i \) and \( j \) (with \( i < j \)) such that the elements at these indices are equal (i.e., \( arr[i] = arr[j] \)) and the difference \( j - i \) is maximized. If there are multiple pairs with the same maximum difference, choose the pair with the smallest \( i \). If no such pair exists, output -1
.
Note: The indices in the output should be 1-indexed.
Example:
- For
n = 6
andarr = [1, 3, 1, 4, 5, 1]
, the answer is1 6
because the first and last occurrence of 1 yield the maximum distance of 5. - For
n = 4
andarr = [1, 2, 3, 4]
, there are no equal elements, so the answer is-1
.
inputFormat
The first line of input contains a single integer \( T \) representing the number of test cases. For each test case, the first line contains an integer \( n \) denoting the size of the array. The second line contains \( n \) space-separated integers which represent the array elements.
outputFormat
For each test case, output the answer in a separate line. If a valid pair is found, print two space-separated integers representing the 1-indexed positions \( i \) and \( j \). Otherwise, print -1
.
2
6
1 3 1 4 5 1
5
7 7 7 7 7
1 6
1 5
</p>