Number of Subarrays With LCM Equal to K - Problem
Given an integer array nums and an integer k, return the number of subarrays of nums where the least common multiple of the subarray's elements is k.
A subarray is a contiguous non-empty sequence of elements within an array.
The least common multiple of an array is the smallest positive integer that is divisible by all the array elements.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [2,6,4], k = 6
›
Output:
2
💡 Note:
The subarrays [6] and [2,6] have LCM equal to 6. LCM of [2,6,4] = 12, LCM of [6,4] = 12, LCM of [4] = 4.
Example 2 — Single Element
$
Input:
nums = [4,4], k = 1
›
Output:
0
💡 Note:
No subarray has LCM equal to 1. Both [4] and [4,4] have LCM = 4.
Example 3 — Multiple Matches
$
Input:
nums = [3,6,2,3], k = 6
›
Output:
4
💡 Note:
Subarrays with LCM = 6: [6], [3,6], [6,2], [3,6,2] all have LCM equal to 6.
Constraints
- 1 ≤ nums.length ≤ 1000
- 1 ≤ nums[i], k ≤ 1000
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Array of integers and target LCM value k
2
Check Subarrays
Find all contiguous subarrays and calculate their LCM
3
Count Matches
Count subarrays where LCM equals k
Key Takeaway
🎯 Key Insight: Only elements that divide k can contribute to subarrays with LCM = k
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code