4032. Longest Subarray With at Most K Distinct Prime Factors
Description
You are given an integer array nums consisting of positive integers and an integer k.
The prime factor set of a subarray is the union of the distinct prime factors of all its elements.
Return the length of the longest subarray whose prime factor set contains at most k distinct prime factors. If no such subarray exists, return 0.
Example 1:
Input: nums = [7,6,10,12,11], k = 3
Output: 3
Explanation:
Consider the subarray [6, 10, 12]:
- The distinct prime factors of 6 are
{2, 3}. - The distinct prime factors of 10 are
{2, 5}. - The distinct prime factors of 12 are
{2, 3}. - The union of these sets is
{2, 3, 5}, which contains 3 distinct prime factors.
No longer subarray satisfies the condition. Therefore, the answer is 3.
Example 2:
Input: nums = [4,6,9,18], k = 4
Output: 4
Explanation:
Consider the entire array [4, 6, 9, 18]:
- The distinct prime factors of 4 are
{2}. - The distinct prime factors of 6 are
{2, 3}. - The distinct prime factors of 9 are
{3}. - The distinct prime factors of 18 are
{2, 3}. - The union of these sets is
{2, 3}, which contains 2 distinct prime factors.
Since 2 <= 4, the entire array is valid. Therefore, the answer is 4.
Example 3:
Input: nums = [6,10,15], k = 2
Output: 1
Explanation:
Every subarray of length at least 2 has prime factor set {2, 3, 5}, which contains 3 distinct prime factors.
Since 3 > 2, only subarrays of length 1 are valid. Therefore, the answer is 1.
Constraints:
1 <= nums.length <= 1052 <= nums[i] <= 1051 <= k <= 104
Solutions
Solution 1: Preprocessing + Sliding Window
Thinking
A subarray is legal if and only if it has at most \(k\) distinct prime factors. That constraint is monotone in the window, so a sliding window applies.
Factoring every value online would multiply \(n\) by \(M=10^5\). A sieve stores the prime-factor lists on \([2,M]\); the window updates a hash table from those lists as it expands or shrinks.
Whenever the number of distinct primes is again at most \(k\), the window length updates the answer.
First, we preprocess the list of prime factors for every number in \([2, 10^5]\) and store them in \(\textit{primes}\). Specifically, we enumerate \(i = 2, 3, \cdots, M\). If \(\textit{primes}[i]\) is empty, then \(i\) is a prime, and we add \(i\) to the prime-factor list of every multiple of \(i\).
Then we use a sliding window to find the longest valid subarray. A hash table \(\textit{cnt}\) records the occurrence count of each prime factor in the current window. When the right pointer \(r\) expands, we add all prime factors of \(\textit{nums}[r]\) to the window. When the number of distinct prime factors in the window exceeds \(k\), the left pointer \(l\) shrinks and we remove the prime factors of \(\textit{nums}[l]\). Whenever the window is valid, we update the answer with the window length.
The time complexity is \(O(M \log \log M + n \log M)\), and the space complexity is \(O(M \log \log M)\), where \(n\) is the length of \(\textit{nums}\) and \(M = 10^5\) is the maximum value of the array elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |