<p>Given a non-empty array of non-negative integers <code>nums</code>, the <b>degree</b> of this array is defined as the maximum frequency of any one of its elements.</p>

<p>Your task is to find the smallest possible length of a (contiguous) subarray of <code>nums</code>, that has the same degree as <code>nums</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,2,3,1]
<strong>Output:</strong> 2
<strong>Explanation:</strong> 
The input array has a degree of 2 because both elements 1 and 2 appear twice.
Of the subarrays that have the same degree:
[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
The shortest length is 2. So return 2.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,2,3,1,4,2]
<strong>Output:</strong> 6
<strong>Explanation:</strong> 
The degree is 3 because the element 2 is repeated 3 times.
So [2,2,3,1,4,2] is the shortest subarray, therefore returning 6.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>nums.length</code> will be between 1 and 50,000.</li>
	<li><code>nums[i]</code> will be an integer between 0 and 49,999.</li>
</ul>

class Solution {
public:
    int findShortestSubArray(vector<int>& arr) {
    int n = arr.size();
    int s = 0;
    int e = 0 ;
    int len = 0; 
    int maxf = 0;
    int maxl = n;
    //int ftemp[50000] = {0};
    //int sidx[50000] = {0};
    int map[50000][2] = {0};
    for (int i=0;i<n;i++ )
    {
        int ele = arr[i];
        map[ele][0] += 1 ;       //update freq 
        if(map[ele][0] == 1){   //if freq is 1 
            map[ele][1] = i ;    //update 1st index for that ele
        }
        int currfreq = map[ele][0] ;
        len = i - map[ele][1] + 1; //calc current lenght 
       
        //update maxlen & maxfreq if :1.current len is less than max_len 
        //          2.freq should be greater than or equal to cuurent freq 
       
        if(currfreq >  maxf || ( currfreq == maxf && len < maxl ) )
        {
            maxl = len ; 
            maxf= currfreq;
            s =  map[ele][1]; 
            e = i;
        }
        //cout<<"for "<<i<<": s " << s << " e "<< e << endl;
    }
    //cout<<" s " << s << " e "<< e << endl;
    return maxl ;
    }
};
27082