Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements.
Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums.
Example 1:
Input: nums = [1,2,2,3,1] Output: 2 Explanation: 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.
Example 2:
Input: nums = [1,2,2,3,1,4,2] Output: 6 Explanation: 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.
Constraints:
nums.length will be between 1 and 50,000.nums[i] will be an integer between 0 and 49,999.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To solve this problem using the brute force approach, we look at every single possible sub-group of items from the start to the end. For each sub-group, we count how often each item appears to figure out the maximum frequency, and then we find the shortest sub-group that shares that same maximum frequency.
Here's how the algorithm would work step-by-step:
def find_shortest_sub_array(nums): # Find the overall degree by counting the maximum frequency of any element in the entire array overall_degree = max([nums.count(item) for item in nums]) if nums else 0 shortest_length = len(nums) # Generate every possible sub-array to examine their frequencies and lengths for start_index in range(len(nums)): for end_index in range(start_index, len(nums)): sub_array = nums[start_index:end_index + 1] sub_array_degree = max([sub_array.count(item) for item in sub_array]) if sub_array else 0 # Only consider sub-arrays that match the overall degree to find the minimum length if sub_array_degree == overall_degree: current_length = (end_index - start_index) + 1 if current_length < shortest_length: shortest_length = current_length return shortest_lengthTo solve this problem efficiently, we need to track how often each item appears and find the shortest stretch between the first and last appearance of the items that appear the most. By keeping notes as we scan through the items just once, we can quickly figure out which items tie for the most frequent and measure their span.
Here's how the algorithm would work step-by-step:
def find_shortest_sub_array(input_numbers):
first_seen_positions = {}
last_seen_positions = {}
frequency_counts = {}
# We scan once to record frequencies and boundaries for every item.
for current_index, current_number in enumerate(input_numbers):
if current_number not in first_seen_positions:
first_seen_positions[current_number] = current_index
last_seen_positions[current_number] = current_index
frequency_counts[current_number] = frequency_counts.get(current_number, 0) + 1
maximum_degree = max(frequency_counts.values())
shortest_span_length = len(input_numbers)
# We evaluate only the items that share the maximum degree to find the minimum span.
for current_number, frequency_count in frequency_counts.items():
if frequency_count == maximum_degree:
current_span_length = last_seen_positions[current_number] - first_seen_positions[current_number] + 1
if current_span_length < shortest_span_length:
shortest_span_length = current_span_length
return shortest_span_length| Case | How to Handle |
|---|---|
| Minimum-sized input with a single element | The degree is 1, so the shortest contiguous subarray of that degree is 1, which the algorithm computes correctly by returning the difference between the first and last indices plus one. |
| Array where all elements are unique | Every element has a frequency of 1, the degree is 1, and the algorithm correctly identifies 1 as the minimum length by taking the first occurrence of any element. |
| Array with multiple elements sharing the maximum frequency | We track the maximum frequency globally and update the minimum length whenever any element matches or exceeds the current degree, correctly capturing all candidates. |
| Input containing negative numbers, zeros, and positive integers | Using hash maps or hash tables instead of direct index arrays allows the solution to seamlessly handle arbitrary integer ranges without out-of-bounds errors. |
| Array with all elements identical | The degree equals the length of the array, and the algorithm returns the full array length since the first and last indices span the entire input. |
| Maximum-sized input arrays leading to large memory or time constraints | A single-pass or two-pass approach using hash maps runs in O(N) time and O(N) space, scaling efficiently to meet performance requirements. |
| Extreme boundary values for integers | Standard hash map implementations natively support all 32-bit or 64-bit signed integers as keys without causing overflow issues. |
| Non-empty constraint violation as described in the problem statement | Since the input is guaranteed to be non-empty, we do not need special guards for empty arrays and can safely initialize our tracking variables. |