Skip to content

Commit 231b204

Browse files
authored
Create 2470-number-of-subarrays-with-lcm-equal-to-k.js
1 parent d3acb0f commit 231b204

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var subarrayLCM = function(nums, k) {
7+
let res = 0
8+
const n = nums.length
9+
10+
for(let i = 0; i < n; i++) {
11+
let tmp = nums[i]
12+
for(let j = i; j < n; j++) {
13+
if(k % nums[j] !== 0) break
14+
if(lcm(tmp, nums[j]) === k) res++
15+
tmp = Math.max(tmp, nums[j])
16+
}
17+
}
18+
19+
return res
20+
};
21+
22+
23+
function lcm(a, b) {
24+
return a * b / gcd(a, b);
25+
}
26+
27+
function gcd(a, b) {
28+
return b ? gcd(b, a % b) : a
29+
}

0 commit comments

Comments
 (0)