/**
* Problem: LeetCode 3934 - Smallest Unique Subarray
* Difficulty: Hard
* Language: Java
* Author: CodeByTushu
*/
class Solution {
private static final long MOD = (1L << 61) - 1;
private long[] h, pw;
private int[] nums;
private int n;
public int smallestUniqueSubarray(int[] nums) {
this.nums = nums;
n = nums.length;
long base = 1_000_003L + new java.util.Random().nextInt(1_000_000);
h = new long[n + 1];
pw = new long[n + 1];
pw[0] = 1;
for (int i = 0; i < n; i++) {
h[i + 1] = (mul(h[i], base) + nums[i]) % MOD;
pw[i + 1] = mul(pw[i], base);
}
// Length n ka subarray hamesha unique hota hai, isliye answer <= n
int lo = 1, hi = n;
while (lo < hi) {
int mid = (lo + hi) >>> 1;
if (hasUnique(mid)) hi = mid;
else lo = mid + 1;
}
return lo;
}
// Kya length L ka koi subarray aisa hai jo sirf ek baar aata hai?
private boolean hasUnique(int L) {
int m = n - L + 1;
long[] arr = new long[m];
for (int i = 0; i < m; i++) {
long v = (h[i + L] - mul(h[i], pw[L])) % MOD;
if (v < 0) v += MOD;
arr[i] = v;
}
java.util.Arrays.sort(arr);
for (int i = 0; i < m; i++) {
boolean sameLeft = i > 0 && arr[i] == arr[i - 1];
boolean sameRight = i < m - 1 && arr[i] == arr[i + 1];
if (!sameLeft && !sameRight) return true;
}
return false;
}
// (a * b) mod (2^61 - 1) using fast bitwise reduction
private static long mul(long a, long b) {
long hi = Math.multiplyHigh(a, b);
long lo = a * b;
long r = (lo & MOD) + ((lo >>> 61) | (hi << 3));
r = (r & MOD) + (r >>> 61);
if (r >= MOD) r -= MOD;
return r;
}
}
"""
Problem: LeetCode 3934 - Smallest Unique Subarray
Difficulty: Hard
Language: Python 3
Author: CodeByTushu
"""
import random
class Solution:
def smallestUniqueSubarray(self, nums: list[int]) -> int:
n = len(nums)
MOD = (1 << 61) - 1
base = 1000003 + random.randint(1, 1000000)
h = [0] * (n + 1)
pw = [1] * (n + 1)
for i in range(n):
h[i + 1] = (h[i] * base + nums[i]) % MOD
pw[i + 1] = (pw[i] * base) % MOD
def has_unique(L: int) -> bool:
m = n - L + 1
pw_L = pw[L]
arr = []
for i in range(m):
v = (h[i + L] - h[i] * pw_L) % MOD
arr.append(v)
arr.sort()
for i in range(m):
same_left = i > 0 and arr[i] == arr[i - 1]
same_right = i < m - 1 and arr[i] == arr[i + 1]
if not same_left and not same_right:
return True
return False
lo, hi = 1, n
while lo < hi:
mid = (lo + hi) // 2
if has_unique(mid):
hi = mid
else:
lo = mid + 1
return lo
/**
* Problem: LeetCode 3934 - Smallest Unique Subarray
* Difficulty: Hard
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <random>
#include <algorithm>
using namespace std;
class Solution {
using u64 = unsigned long long;
using u128 = __uint128_t;
const u64 MOD = (1ULL << 61) - 1;
vector<u64> h, pw;
int n;
u64 mul(u64 a, u64 b) {
u128 r = (u128)a * b;
u64 res = (r & MOD) + (r >> 61);
if (res >= MOD) res -= MOD;
return res;
}
bool hasUnique(int L) {
int m = n - L + 1;
vector<u64> arr(m);
u64 pwL = pw[L];
for (int i = 0; i < m; i++) {
u64 sub = mul(h[i], pwL);
u64 v = (h[i + L] >= sub) ? (h[i + L] - sub) : (h[i + L] + MOD - sub);
arr[i] = v;
}
sort(arr.begin(), arr.end());
for (int i = 0; i < m; i++) {
bool sameLeft = (i > 0 && arr[i] == arr[i - 1]);
bool sameRight = (i < m - 1 && arr[i] == arr[i + 1]);
if (!sameLeft && !sameRight) return true;
}
return false;
}
public:
int smallestUniqueSubarray(vector<int>& nums) {
n = nums.size();
mt19937_64 rng(1337);
u64 base = 1000003ULL + (rng() % 1000000ULL);
h.resize(n + 1, 0);
pw.resize(n + 1, 1);
for (int i = 0; i < n; i++) {
h[i + 1] = (mul(h[i], base) + (u64)nums[i]) % MOD;
pw[i + 1] = mul(pw[i], base);
}
int lo = 1, hi = n;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (hasUnique(mid)) hi = mid;
else lo = mid + 1;
}
return lo;
}
};
/**
* Problem: LeetCode 3934 - Smallest Unique Subarray
* Difficulty: Hard
* Language: JavaScript
* Author: CodeByTushu
*/
/**
* @param {number[]} nums
* @return {number}
*/
var smallestUniqueSubarray = function(nums) {
const n = nums.length;
const MOD = (1n << 61n) - 1n;
const base = 1000003n + BigInt(Math.floor(Math.random() * 1000000));
const h = new BigUint64Array(n + 1);
const pw = new BigUint64Array(n + 1);
pw[0] = 1n;
for (let i = 0; i < n; i++) {
h[i + 1] = (h[i] * base + BigInt(nums[i])) % MOD;
pw[i + 1] = (pw[i] * base) % MOD;
}
const hasUnique = (L) => {
const m = n - L + 1;
const pwL = pw[L];
const arr = new BigUint64Array(m);
for (let i = 0; i < m; i++) {
let v = (h[i + L] - ((h[i] * pwL) % MOD)) % MOD;
if (v < 0n) v += MOD;
arr[i] = v;
}
arr.sort();
for (let i = 0; i < m; i++) {
const sameLeft = i > 0 && arr[i] === arr[i - 1];
const sameRight = i < m - 1 && arr[i] === arr[i + 1];
if (!sameLeft && !sameRight) return true;
}
return false;
};
let lo = 1, hi = n;
while (lo < hi) {
const mid = (lo + hi) >> 1;
if (hasUnique(mid)) {
hi = mid;
} else {
lo = mid + 1;
}
}
return lo;
};
TIME COMPLEXITY
O(N * log^2 N)
SPACE COMPLEXITY
O(1)
TOPICS
Binary Search
Binary Search Tree
String
String Matching
Subsequence
Tree / Binary Tree