Smallest sub-array with all occurences of most frequent element
Submissions: 876   Accuracy:

15.12%

  Difficulty: Easy   Marks: 2

Given an array, A.Let x be an element in the array which has the maximum frequency in the array.Find the smallest subsegment of the array which also has x as the maximum frequency element.
NOTE : If there are two or more such sub-arrays then print the sub-array that is ending first(last index is least).
Examples:

 

Input :  arr[] = {4, 1, 1, 2, 2, 1, 3, 3} 
Output :   1, 1, 2, 2, 1
The most frequent element is 1. The smallest
subarray that has all occurrences of it is
1 1 2 2 1

Input :  A[] = {1, 2, 2, 3, 1}
Output : 2, 2
Note that there are two elements that appear
two times, 1 and 2. The smallest window for
1 is whole array and smallest window for 2 is
{2, 2}. Since window for 2 is smaller, this is
our output.

INPUT : The first line consists of an integer T i.e. the number of test cases. First line of each test case consists of a integer n. Second and last line of each test case consists of n spaced integers.

OUTPUT : Print the required sub-array.

CONSTRAINTS : 
1<=T<=100
1<=n,a[i]<=104

EXAMPLES :
INPUT :

2
5
1 2 1 1 2 

3 2 5 2 3 3 

OUTPUT :
1 2 1 1 
3 2 5 2 3 3 

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Saksham Ashtputre
Author: saksham0751


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.