Intuition
If a short window is unique, extending it keeps it unique, so the answer is monotonic: binary search the smallest length for which some window appears exactly once, testing each length with rolling-hash fingerprints.
Explanation
Reframing the problem
We want the smallest length L such that some subarray of length L appears exactly once in nums. Call such a length feasible. The answer is the smallest feasible length.
The monotonic insight
Feasibility is monotonic in L. Suppose a window W of length L is unique. Extend it by one element on the right to get a window W' of length L+1. If W' equalled some other length-(L+1) window at position j, then their first L elements would match too, giving a second copy of W -- contradicting that W was unique. So W' is also unique, meaning if length L is feasible, so is length L+1.
The full array (length n) is the only subarray of that length, so it is always unique -- length n is always feasible, and an answer always exists.
Because feasibility flips from false to true at a single threshold, we can binary search the smallest feasible length.
Testing one length quickly
To test a length L we need to know whether any window of length L occurs exactly once. Comparing windows element by element is too slow, so we fingerprint each window with a polynomial rolling hash built from prefix hashes: the hash of nums[i..i+L) is computed in O(1) from precomputed prefixes. We tally how many windows share each fingerprint; L is feasible iff some fingerprint has a count of 1. Using two independent hashes makes an accidental collision negligible.
Walk-through with nums = [1,1,2,2,1]
- Length 1: windows are
[1],[1],[2],[2],[1]. Counts:1appears 3 times,2appears 2 times. No window is unique, so length 1 is not feasible. - Length 2: windows are
[1,1],[1,2],[2,2],[2,1]. Every one is distinct, so each count is 1 -- length 2 is feasible. - Binary search over lengths 1..5 confirms the first feasible length is 2, so the answer is
2.
Complexity
- Time: O(n log n) -- each of the O(log n) binary-search steps slides a window across the array in O(n).
- Space: O(n) for the prefix-hash arrays and the per-length tally.
Implementation
class Solution {
private int n;
private long[] h1, h2, p1, p2;
private static final long MOD1 = 1_000_000_007L, MOD2 = 998_244_353L;
public int smallestUniqueSubarray(int[] nums) {
n = nums.length;
long B1 = 131, B2 = 137;
h1 = new long[n + 1];
h2 = new long[n + 1];
p1 = new long[n + 1];
p2 = new long[n + 1];
p1[0] = 1;
p2[0] = 1;
for (int i = 0; i < n; i++) {
h1[i + 1] = (h1[i] * B1 + nums[i]) % MOD1;
h2[i + 1] = (h2[i] * B2 + nums[i]) % MOD2;
p1[i + 1] = (p1[i] * B1) % MOD1;
p2[i + 1] = (p2[i] * B2) % MOD2;
}
// Feasibility is monotonic in length, and the whole array is always
// unique, so binary search the smallest length that works.
int lo = 1, hi = n;
while (lo < hi) {
int mid = (lo + hi) / 2;
if (feasible(mid)) hi = mid;
else lo = mid + 1;
}
return lo;
}
private boolean feasible(int length) {
java.util.HashMap<Long, Integer> seen = new java.util.HashMap<>();
for (int i = 0; i + length <= n; i++) {
long v1 = ((h1[i + length] - h1[i] * p1[length]) % MOD1 + MOD1) % MOD1;
long v2 = ((h2[i + length] - h2[i] * p2[length]) % MOD2 + MOD2) % MOD2;
long key = v1 * 1_000_000_009L + v2;
seen.merge(key, 1, Integer::sum);
}
for (int c : seen.values()) {
if (c == 1) return true;
}
return false;
}
}