`{"id":"cc090f42-a658-402a-bb01-882de2caced7","name":"Smallest Subarray With All Occurrences Of The Most Frequent Element ","description":"1. You are given an array(arr) of integers.\r\n2. You have to find the element(x) with maximum frequency in the given array. \r\n3. Also, you have to find the smallest subarray which has all occurrences of the most frequent element i.e x.\r\n\r\nNote -> If there are two or more elements with maximum frequency and the same subarray size then print the subarray which occurs first in the given array.","inputFormat":"A number N\r\narr1\r\narr2.. N numbers","outputFormat":"A number(x) representing the maximum frequency element in the given array.\r\nTwo numbers representing the start and end index of the smallest subarray with all occurrences of the most frequent element.","constraints":"1 &lt;= N &lt;= 10^6\r\n1 &lt;= arr[i] &lt;= 10^5","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvoid solution(vector<int> arr) {\n\t// write your code here\n}\nint main() {\n\tint n;\n\tcin>>n;\n\tvector<int> arr(n);\n\tfor(int i = 0 ; i < n; i++){\n\t\tcin >> arr[i];\n\t}\n\tsolution(arr);\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void solution(int[] arr) {\r\n // write your code here\r\n \r\n return 0;\r\n }\r\n \r\n public static void main(String[] args) {\r\n\t\tScanner scn = new Scanner(System.in);\r\n\t\tint n = scn.nextInt();\r\n\t\tint[] arr = new int[n];\r\n\t\tfor(int i = 0 ; i < n; i++){\r\n\t\t\tarr[i] = scn.nextInt();\r\n\t\t}\r\n solution(arr);\r\n\t}\r\n\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"8\r\n4 1 1 2 2 1 3 3","sampleOutput":"1\r\n1\r\n5\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"ec6515dc-6811-4c65-912f-0134b65df120","name":"HashMap and Heap For Intermediate","slug":"hashmap-and-heap-for-intermediate-10002","type":0},{"id":"addd6504-9e90-49b0-8b77-6735eac8e5d2","name":"Smallest Subarray With All Occurrences Of The Most Frequent Element ","slug":"smallest-subarray-with-all-occurrences-of-the-most-frequent-element","type":1}],"next":{"id":"1f19573a-1d76-4132-9cd2-c4aaa7950f4e","name":"Smallest Subarray With All Occurrences Of The Most Frequent Element MCQ","type":0,"slug":"smallest-subarray-with-all-occurrences-of-the-most-frequent-element-mcq"},"prev":{"id":"46b1ab16-a0e9-4de0-b619-0255e03e5a24","name":"Check Arithmetic Sequence","type":3,"slug":"check-arithmetic-sequence"}}}`

# Smallest Subarray With All Occurrences Of The Most Frequent Element

1. You are given an array(arr) of integers. 2. You have to find the element(x) with maximum frequency in the given array. 3. Also, you have to find the smallest subarray which has all occurrences of the most frequent element i.e x. Note -> If there are two or more elements with maximum frequency and the same subarray size then print the subarray which occurs first in the given array.

`{"id":"cc090f42-a658-402a-bb01-882de2caced7","name":"Smallest Subarray With All Occurrences Of The Most Frequent Element ","description":"1. You are given an array(arr) of integers.\r\n2. You have to find the element(x) with maximum frequency in the given array. \r\n3. Also, you have to find the smallest subarray which has all occurrences of the most frequent element i.e x.\r\n\r\nNote -> If there are two or more elements with maximum frequency and the same subarray size then print the subarray which occurs first in the given array.","inputFormat":"A number N\r\narr1\r\narr2.. N numbers","outputFormat":"A number(x) representing the maximum frequency element in the given array.\r\nTwo numbers representing the start and end index of the smallest subarray with all occurrences of the most frequent element.","constraints":"1 &lt;= N &lt;= 10^6\r\n1 &lt;= arr[i] &lt;= 10^5","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvoid solution(vector<int> arr) {\n\t// write your code here\n}\nint main() {\n\tint n;\n\tcin>>n;\n\tvector<int> arr(n);\n\tfor(int i = 0 ; i < n; i++){\n\t\tcin >> arr[i];\n\t}\n\tsolution(arr);\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void solution(int[] arr) {\r\n // write your code here\r\n \r\n return 0;\r\n }\r\n \r\n public static void main(String[] args) {\r\n\t\tScanner scn = new Scanner(System.in);\r\n\t\tint n = scn.nextInt();\r\n\t\tint[] arr = new int[n];\r\n\t\tfor(int i = 0 ; i < n; i++){\r\n\t\t\tarr[i] = scn.nextInt();\r\n\t\t}\r\n solution(arr);\r\n\t}\r\n\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"8\r\n4 1 1 2 2 1 3 3","sampleOutput":"1\r\n1\r\n5\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"ec6515dc-6811-4c65-912f-0134b65df120","name":"HashMap and Heap For Intermediate","slug":"hashmap-and-heap-for-intermediate-10002","type":0},{"id":"addd6504-9e90-49b0-8b77-6735eac8e5d2","name":"Smallest Subarray With All Occurrences Of The Most Frequent Element ","slug":"smallest-subarray-with-all-occurrences-of-the-most-frequent-element","type":1}],"next":{"id":"1f19573a-1d76-4132-9cd2-c4aaa7950f4e","name":"Smallest Subarray With All Occurrences Of The Most Frequent Element MCQ","type":0,"slug":"smallest-subarray-with-all-occurrences-of-the-most-frequent-element-mcq"},"prev":{"id":"46b1ab16-a0e9-4de0-b619-0255e03e5a24","name":"Check Arithmetic Sequence","type":3,"slug":"check-arithmetic-sequence"}}}`

Editor

# Smallest Subarray With All Occurrences Of The Most Frequent Element

medium

1. You are given an array(arr) of integers. 2. You have to find the element(x) with maximum frequency in the given array. 3. Also, you have to find the smallest subarray which has all occurrences of the most frequent element i.e x. Note -> If there are two or more elements with maximum frequency and the same subarray size then print the subarray which occurs first in the given array.

## Constraints

1 <= N <= 10^6 1 <= arr[i] <= 10^5

## Format

### Input

A number N arr1 arr2.. N numbers

### Output

A number(x) representing the maximum frequency element in the given array. Two numbers representing the start and end index of the smallest subarray with all occurrences of the most frequent element.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}8 4 1 1 2 2 1 3 3```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}1 1 5 ```

Discussions

Show Discussion

Related Resources