Explanation:
The degree is 3 because the element 2 appears 3 times.
The shortest subarray that has the same degree is [2, 2, 3, 1, 4, 2], so the output is 6.
Constraints:
nums.length will be between 1 and 50,000.
nums[i] will be an integer between 0 and 49,999.
Approach:
To solve this problem efficiently:
Track the frequency of each element to determine the degree of the array.
Use a hashmap to store the first and last occurrence of each element to calculate the length of subarrays.
Identify the elements that contribute to the degree and find the smallest subarray length that has the same frequency.
Assistant
Q- Can you explain the problem using your own words?
We have a definition, the degree of an array represents the maxi number of frequency of the
Element in the array
For example
nums = [1, 2, 2, 3, 1]
The degree of this array is equal to 2 ( 2 is repeated two times, 1 is repeated two times )
Our task is to find the smallest sub-continuous array where that has the same degree
For example [1,2,2] is of the same degree but not the smallest possible array
[2,2] is the smallest one so we return two
Q- what is the input/output?
Input : List[int]
Output: int
Q- can you through an example ?
[1,2,2,3,1]
Q- give me all the possible subarray of this is input array
Sure! Let's go through the example with nums = [1, 2, 2, 3, 1]. Here are all possible contiguous subarrays:
[1]
[1, 2]
[1, 2, 2]
[1, 2, 2, 3]
[1, 2, 2, 3, 1]
[2]
[2, 2]
[2, 2, 3]
[2, 2, 3, 1]
[2, 3]
[2, 3, 1]
[3]
[3, 1]
[1]
Now, you want to find the smallest contiguous subarray that has the same degree as the original array:
The degree of the array is 2, because the elements 1 and 2 both appear twice.
We look for subarrays that include either 1 or 2 with the same frequency.
The shortest subarray that gives a degree of 2 is [2, 2] (since the element 2 appears twice in this subarray).
Therefore, the smallest subarray length is 2.
Thus, the output would be 2.
Q- First we need to find the degree of the original input array right?
hashmap={1:2,2:2,3:1}
The degree is the max value of the hashmap → 2
Q- the next part is to go through the search process of the smallest possible subarray.
Using two pointers i and j
I : responsible of specifying the left bound
J : responsible of specifying the right bound oky
For each possible array :
Compute the hashmap of the current subarray
Get the rank as the maximum frequency
Get the length of the subarray
If rank of the current subarray == rank the input array
Update the input based on the min length of it
Q- develop for me a good pesuodCode for this ?
Certainly! Let's develop a clear and efficient pseudocode to solve the Degree of an Array problem. We'll follow an optimal approach that minimizes computational complexity, ensuring it works efficiently even for large input sizes (up to 50,000 elements).
Optimal Approach Overview
Calculate the Degree of the Array:
Iterate through the array to count the frequency of each element.
Determine the maximum frequency (degree) from these counts.
Track First and Last Occurrences:
While counting frequencies, also record the first and last index where each element appears.
This information helps in identifying the smallest subarray that maintains the degree.
Determine the Smallest Subarray Length:
For each element that contributes to the degree (i.e., has a frequency equal to the degree), calculate the length of the subarray spanning from its first to last occurrence.
Keep track of the minimum length encountered during this process.
Pseudocode
plaintext
FUNCTION findDegreeOfArray(nums):
// Step 1: Initialize necessary data structures
CREATE a hashmap 'frequencyMap' to store frequency of each element
CREATE a hashmap 'firstIndexMap' to store first occurrence index of each element
CREATE a hashmap 'lastIndexMap' to store last occurrence index of each element
// Step 2: Populate the maps
FOR index FROM 0 TO LENGTH(nums) - 1:
element = nums[index]
// Update frequency
IF element NOT IN frequencyMap:
frequencyMap[element] = 1
firstIndexMap[element] = index // Record first occurrence
ELSE:
frequencyMap[element] += 1
// Update last occurrence
lastIndexMap[element] = index
// Step 3: Determine the degree of the array
degree = 0
FOR EACH key IN frequencyMap:
IF frequencyMap[key] > degree:
degree = frequencyMap[key]
// Step 4: Find the smallest subarray length with the same degree
minLength = LENGTH(nums) // Initialize with maximum possible length
FOR EACH key IN frequencyMap:
IF frequencyMap[key] == degree:
currentLength = lastIndexMap[key] - firstIndexMap[key] + 1
IF currentLength < minLength:
minLength = currentLength
RETURN minLength
Explanation of the Pseudocode
Initialization:
frequencyMap: Keeps track of how many times each element appears.
firstIndexMap: Records the first index where each element appears.
lastIndexMap: Records the last index where each element appears.
Populating the Maps:
Iterate through the array once.
For each element:
If it's the first occurrence, initialize its frequency and record its first index.
Increment its frequency.
Continuously update its last index to the current index.
Determining the Degree:
After populating the frequencyMap, find the maximum frequency value, which represents the degree of the array.
Finding the Smallest Subarray:
Iterate through the frequencyMap.
For each element that has a frequency equal to the degree:
Calculate the length of the subarray from its first to last occurrence.
Keep track of the minimum such length.
Return the Result:
The smallest length found in the previous step is the desired output.
Example Walkthrough
Let's apply the pseudocode to Example 1:
Input: nums = [1, 2, 2, 3, 1]
Maps After Iteration:
frequencyMap: {1: 2, 2: 2, 3: 1}
firstIndexMap: {1: 0, 2: 1, 3: 3}
lastIndexMap: {1: 4, 2: 2, 3: 3}
Degree Calculation:
Degree = 2 (both 1 and 2 appear twice)
Smallest Subarray Calculation:
For element 1: Subarray from index 0 to 4 → Length 5
For element 2: Subarray from index 1 to 2 → Length 2
Minimum Length = 2
Output: 2
This aligns with the expected output.
Final Notes
Time Complexity: O(n), where n is the length of the input array. This is because we iterate through the array a constant number of times.
Space Complexity: O(n), due to the additional hashmaps used to store frequencies and indices.
This approach ensures that the solution is both efficient and scalable, making it suitable for large inputs within the given constraints.