Question: Given an array of n integers, we define its degree as the maximum frequency of any element in the array. For example, the array(1, 2,

Given an array of n integers, we define its degree as the maximum frequency of any element in the array. For example, the array(1, 2, 3, 4, 2, 2, 3] has a degree of 3 because the number 2 occurs three times (which is more than any other number in the array). We want to know the size of the smallest subarray of our array such that the subarray's degree is equal to the array's degree. For example, the array[1, 2, 2, 3, 1] has a degree of 2 because 1 and 2 occur a maximal two times. There are two possible subarrays with this degree:[1, 2, 2, 3, 1) and (2,2]. Our answer is the length of the smallest subarray, which is 2. Complete the function in the editor below. It has one parameter: an array of n integers, arr. The function must return an integer denoting the minimum size of the subarray such that the degree of the subarray is equal to the degree of the array. Example 1: Input: 12231 Output: 2 The array(1, 2, 2, 3, 1] has a degree of 2 because 1 and 2 both occur two times. Our subarrays with a degree of 2 are: . [1, 2, 2, 3, 1] which has a length of 5. . [2, 2] which has a length of 2. Example 2: Input: 112 122 Output: 4 The array(1, 1, 2, 1, 2, 2] has a degree of 3 because 1 and 2 both occur three times. Our subarrays with a degree of 3 are: . [1, 1, 2, 1] which has a length of 4. [2, 1, 2, 2] which has a length of 4. We then return the minimum length of any subarray with a degree of 3, which is 4
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
