


You are given ‘ARR’ = {1, 3, 4, 3, 5}. Then our answer will be {3, 4, 3} because the most frequent element is 3, and the subarray {3, 4, 3} contains all the occurrences of 3.
The first line contains an integer 'T' which denotes the number of test cases.
The first line of each test case contains an integer 'N' denoting the size of the ‘arr’.
The second contains ‘N’ space-separated integers representing the elements of the array ‘arr’.
For each test case, print the elements of the special subarray separated a space.
The output of each test case will be printed in a separate line.
1 <= T <= 10
1 <= N <= 5000
0 <= ARR[i] <= 10 ^ 6
Time limit: 1 sec
You do not need to input or print anything, as it has already been taken care of. Just implement the given function.
In this approach, we will keep track of the most frequent element using a HashMap. HashMap will contain key-value pairs where the key will be the distinct element of the ‘ARR’ and value will be its frequency. Now, suppose X is the most frequent element. In that case, the special subarray will look like {X, …, X} because we can just remove any other preceding or succeeding element without affecting our answer as we want our special subarray to be the smallest possible. To obtain this subarray, we can just find the index of the first and the last occurrence of the most frequent element. We also have to keep track of the size of the subarrays for the case when the ‘ARR’ contains more than one element with maximum frequency.
Algorithm: