Smallest Unique Subarray - Video Solutions
LeetCode 3934 | Smallest Unique Subarray | Binary Search + Rolling Hash | Java | Hindi
Smallest Unique Subarray - Video Solution
Watch 2 video solutions for Smallest Unique Subarray, a hard level problem involving Array, Hash Table, Binary Search. This walkthrough by Code Kage has 500 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Problem Statement
You are given an integer array nums.
Find the minimum length of a subarray that is not identical to any other subarray in nums.
Return an integer denoting the minimum possible length of such a subarray.
Two subarrays are considered identical if they have the same length and the same elements in corresponding positions.
Example 1:
Input: nums = [3,3,3]
Output: 3
Explanation:
- Subarrays of length 1:
[3]→ appears 3 times - Subarrays of length 2:
[3, 3]→ appears 2 times - Subarrays of length 3:
[3, 3, 3]→ appears once
The subarray [3, 3, 3] is unique, so the smallest unique subarray length is 3.
Example 2:
Input: nums = [2,1,2,3,3]
Output: 1
Explanation:
Subarrays of length 1:
[2]→ appears 2 times[1]→ appears once[3]→ appears 2 times
[1] is unique, so the smallest unique subarray length is 1.Example 3:
Input: nums = [1,1,2,2,1]
Output: 2
Explanation:
Subarrays of length 1:
[1]→ appears 3 times[2]→ appears 2 times
Subarrays of length 2:
[1, 1]→ appears once[1, 2]→ appears once[2, 2]→ appears once[2, 1]→ appears once
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 105
Approach Overview
Problem Overview: You are given an array and need the length of the smallest contiguous subarray that contains every distinct element present in the entire array at least once. The challenge is minimizing the window while ensuring all unique values are covered.
Approach 1: Brute Force Enumeration (O(n^3) time, O(n) space)
Start by identifying the set of all distinct elements in the array. Then enumerate every possible subarray using two nested loops. For each candidate subarray, build a temporary set and check whether it contains all unique elements. This approach repeatedly scans overlapping ranges, which leads to cubic time complexity in the worst case. It is mainly useful for verifying correctness on small inputs or building intuition before optimizing.
Approach 2: Expanding Subarray with Set Tracking (O(n^2) time, O(n) space)
Instead of recomputing the set for each subarray, fix the left boundary and expand the right boundary while maintaining a set of elements currently in the window. Once the window contains all distinct values, update the minimum length. Then move the left boundary and repeat the process. This avoids rebuilding sets from scratch but still scans many overlapping ranges, giving quadratic time complexity. It works for moderate input sizes but struggles when n grows large.
Approach 3: Sliding Window with Frequency Map (O(n) time, O(n) space)
The optimal strategy uses a sliding window combined with a frequency map. First compute the number of distinct elements in the entire array. Then maintain two pointers left and right. As you move right, update a hash map counting occurrences inside the window. When the window contains all distinct elements, shrink it from the left while preserving the condition. Each element enters and leaves the window at most once, producing linear time complexity.
The key insight is that once the window satisfies the requirement, any extra occurrences at the left can be removed without losing coverage. This shrinking step ensures the window remains minimal at every stage. The approach relies on fast hash lookups and the classic two pointers pattern.
Recommended for interviews: Interviewers expect the sliding window solution. Starting with brute force shows you understand the requirement, but quickly transitioning to the O(n) window approach demonstrates strong algorithmic thinking. The ability to maintain counts, detect when all unique elements are covered, and shrink the window correctly is the key signal of skill.
Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Enumeration | O(n^3) | O(n) | Conceptual baseline or very small arrays |
| Expanding Subarray with Set | O(n^2) | O(n) | Moderate input sizes where simpler logic is preferred |
| Sliding Window with Frequency Map | O(n) | O(n) | Optimal general solution for large arrays and interview settings |