Smallest subarray with all occurrences of a most frequent element
Given an array, A.Let x be an element in the array.x has the maximum frequency in the array.Find the smallest subsegment of the array which also has x as the maximum frequency element.
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.
classSolution {publicint[] smallestSubsegment(int[] nums) {// Frequency mapHashMap<Integer,Integer> map =newHashMap<>();int maxFreq =0;for (int x : nums) {map.put(x,map.getOrDefault(x,0) +1); maxFreq =Math.max(maxFreq,map.get(x)); }// Map of elements with max freq -> [lower limit,upper limit]Map<Integer,int[]> maxFreqMap =newHashMap<>();for (int i =0; i <nums.length; i++) {int x = nums[i];if (map.get(x) == maxFreq) {maxFreqMap.computeIfAbsent(x, k ->newint[] { Integer.MAX_VALUE,Integer.MIN_VALUE });maxFreqMap.get(x)[0] =Math.min(maxFreqMap.get(x)[0], i);maxFreqMap.get(x)[1] =Math.max(maxFreqMap.get(x)[1], i); } }// Finding the smallest span, which contains maxFreq elementint start =-1, end =-1;for (int x :maxFreqMap.keySet()) {if ((start ==-1&& end ==-1) || (end - start) > (maxFreqMap.get(x)[1] -maxFreqMap.get(x)[0])) { start =maxFreqMap.get(x)[0]; end =maxFreqMap.get(x)[1]; } }int[] ans =newint[end - start +1];for (int i =0; i < end - start +1; i++) { ans[i] = nums[i + start]; }return ans; }}