Smallest Integer Divisible by All Values in Every Subarray of Length K
Math & Bit ManipulationAuto-graded · 17 test casesLast updated
Tests the ability to combine sliding-window enumeration with LCM/GCD reasoning to find the minimum positive integer that is simultaneously divisible by every contiguous subarray LCM.
Subarray-LCM Coverage
You are given a list of positive integers nums and a positive integer k.
For every contiguous subarray of nums of length exactly k, compute the LCM (least common multiple) of its elements.
Return the LCM of all those per-subarray LCMs — in other words, the smallest positive integer that is divisible by every element in every length-k contiguous subarray of nums.
If k is greater than the length of nums, there are no valid subarrays; return 1 in that case.
How it works
- Slide a window of size
kacrossnums, computing the LCM of each window. - Compute the LCM of all those window-LCMs.
- Return that single integer.
Constraints
1 ≤ len(nums) ≤ 20001 ≤ nums[i] ≤ 10001 ≤ k ≤ 2000- The answer is always a positive integer (it may be very large; Python handles big integers natively).
- If
k > len(nums), return1.
Examples
Example 1
Input: subarray_lcm_coverage([2, 3, 4, 6], 2)
Output: 12
Example 2
Input: subarray_lcm_coverage([1, 2, 3, 4, 5], 3)
Output: 60
Example 3
Input: subarray_lcm_coverage([6], 2)
Output: 1
Hints
Hint 1
Recall that lcm(a, b) = a * b // gcd(a, b). Python's math module provides math.gcd directly.
Hint 2
Slide a window of size k across the array; for each window, fold lcm over its k elements to get that window's LCM. Then fold lcm over all window-LCMs.
Hint 3
Think about why the final answer equals the LCM of every element that appears in at least one valid window — elements outside all windows never affect divisibility.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight
The LCM of all window-LCMs equals the LCM of all elements that appear in at least one valid window of size k. Because every valid window contributes its elements to the running LCM, no element outside a valid window (there are none, by definition) can affect the answer. The outer LCM just accumulates prime factors from all reachable elements.
Algorithm
- Edge case: If
k > len(nums), no window of sizekexists; the answer is the empty LCM, which is1by convention. - Slide: For each starting index
iin[0, n-k], computewindow_lcm = lcm(nums[i], nums[i+1], ..., nums[i+k-1])by foldinglcmover the window starting from1. - Accumulate: Fold
lcmover allwindow_lcmvalues, again starting from1. - Return the accumulated result.
lcm(a, b) = a * b // gcd(a, b) is used throughout; Python's math.gcd handles this efficiently.
Correctness
- Each window LCM is the smallest number divisible by all elements in that window.
- Taking the LCM across all window LCMs gives the smallest number divisible by every window LCM simultaneously, which is exactly the smallest number divisible by every element in any valid window.
Complexity
- Time: O((n − k + 1) · k · log(M)) where M is the maximum value (≤ 1000). With n, k ≤ 2000 and M ≤ 1000 this is at most ~4 × 10⁶ cheap GCD operations — well within limits.
- Space: O(1) extra (beyond the input).
Reference Solution
from math import gcd
from typing import List
def subarray_lcm_coverage(nums: List[int], k: int) -> int:
n = len(nums)
if k > n:
return 1
def lcm(a: int, b: int) -> int:
return a * b // gcd(a, b)
# Compute LCM of each window of size k, then LCM of all window-LCMs.
result = 1
for i in range(n - k + 1):
window_lcm = 1
for j in range(i, i + k):
window_lcm = lcm(window_lcm, nums[j])
result = lcm(result, window_lcm)
return result