Smallest Unique Subarray Problem
Description
LeetCode Problem 3934.
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
The subarray [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
There is at least one subarray of length 2 that is unique, so the smallest unique subarray length is 2.
Constraints:
1 <= nums.length <= 10^51 <= nums[i] <= 10^5
Solution
Approach: Binary Search on the Length + Rolling Hash
If a subarray of length L appears only once, every longer subarray that contains it also appears only once. So "some subarray of length L is unique" is monotone in L, and we can binary search the smallest such L (length n always works).
To test one length, compute the hash of every window of that length with a rolling hash and count how often each hash appears; a length works when some hash appears exactly once. To make collisions practically impossible each window is identified by a pair of hashes under two different prime moduli (below 108, so every product fits in a 64-bit integer, and the pair even fits in a JavaScript number). The prefix hashes and powers are computed once.
Complexity: O(n log n) time (one pass per binary-search step, plus sorting or hashing the keys) and O(n) extra space.
from collections import Counter
class Solution:
def smallestUniqueSubarray(self, nums: List[int]) -> int:
n = len(nums)
p1, p2 = 94906249, 94906247 # two moduli: a window is identified by its pair of rolling hashes
b1, b2 = 91138, 97531
h1, h2 = [0] * (n + 1), [0] * (n + 1)
w1, w2 = [1] * (n + 1), [1] * (n + 1)
for i, x in enumerate(nums):
h1[i + 1] = (h1[i] * b1 + x) % p1
h2[i + 1] = (h2[i] * b2 + x) % p2
w1[i + 1] = w1[i] * b1 % p1
w2[i + 1] = w2[i] * b2 % p2
def has_unique(length: int) -> bool:
cnt = Counter()
for i in range(n - length + 1):
a = (h1[i + length] - h1[i] * w1[length]) % p1
b = (h2[i + length] - h2[i] * w2[length]) % p2
cnt[a * p2 + b] += 1
return 1 in cnt.values()
# If a subarray of length L is unique, so is any longer one containing it, so we can binary search.
lo, hi = 1, n
while lo < hi:
mid = (lo + hi) // 2
if has_unique(mid):
hi = mid
else:
lo = mid + 1
return lo