Skip to content

Commit e66b777

Browse files
authored
Create 1819-number-of-different-subsequences-gcds.js
1 parent c3c43be commit e66b777

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const countDifferentSubsequenceGCDs = function(nums) {
6+
const MAX = 2e5 + 1;
7+
const cnt = Array(MAX).fill(0)
8+
for (let x of nums) cnt[x] = true;
9+
let ret = 0;
10+
for (let x=1; x<MAX; x++) {
11+
let g = 0;
12+
for (let y=x; y<MAX; y+=x) {
13+
if (cnt[y]) g = gcd(g, y);
14+
}
15+
if (g == x) ret++;
16+
}
17+
return ret;
18+
};
19+
20+
function gcd(x,y){
21+
if(y === 0) return x
22+
return gcd(y, x % y)
23+
}
24+
25+

0 commit comments

Comments
 (0)