Skip to content

Commit 2fa598d

Browse files
committed
feat: add solution 1819. Number of Different Subsequences GCDs
1 parent 24986ad commit 2fa598d

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# [1819. Number of Different Subsequences GCDs](https://leetcode.com/problems/number-of-different-subsequences-gcds)
2+
3+
## Description
4+
5+
<div class="elfjS" data-track-load="description_content"><p>You are given an array <code>nums</code> that consists of positive integers.</p>
6+
7+
<p>The <strong>GCD</strong> of a sequence of numbers is defined as the greatest integer that divides <strong>all</strong> the numbers in the sequence evenly.</p>
8+
9+
<ul>
10+
<li>For example, the GCD of the sequence <code>[4,6,16]</code> is <code>2</code>.</li>
11+
</ul>
12+
13+
<p>A <strong>subsequence</strong> of an array is a sequence that can be formed by removing some elements (possibly none) of the array.</p>
14+
15+
<ul>
16+
<li>For example, <code>[2,5,10]</code> is a subsequence of <code>[1,2,1,<strong><u>2</u></strong>,4,1,<u><strong>5</strong></u>,<u><strong>10</strong></u>]</code>.</li>
17+
</ul>
18+
19+
<p>Return <em>the <strong>number</strong> of <strong>different</strong> GCDs among all <strong>non-empty</strong> subsequences of</em> <code>nums</code>.</p>
20+
21+
<p>&nbsp;</p>
22+
<p><strong class="example">Example 1:</strong></p>
23+
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/17/image-1.png" style="width: 149px; height: 309px;">
24+
<pre><strong>Input:</strong> nums = [6,10,3]
25+
<strong>Output:</strong> 5
26+
<strong>Explanation:</strong> The figure shows all the non-empty subsequences and their GCDs.
27+
The different GCDs are 6, 10, 3, 2, and 1.
28+
</pre>
29+
30+
<p><strong class="example">Example 2:</strong></p>
31+
32+
<pre><strong>Input:</strong> nums = [5,15,40,5,6]
33+
<strong>Output:</strong> 7
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
41+
<li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>5</sup></code></li>
42+
</ul>
43+
</div>
44+
45+
<p>&nbsp;</p>
46+
47+
## Solutions
48+
49+
**Solution: `Math`**
50+
51+
- Time complexity: <em>O(nlogn)</em>
52+
- Space complexity: <em>O(n)</em>
53+
54+
<p>&nbsp;</p>
55+
56+
### **JavaScript**
57+
58+
```js
59+
/**
60+
* @param {number[]} nums
61+
* @return {number}
62+
*/
63+
const countDifferentSubsequenceGCDs = function (nums) {
64+
const maxNum = Math.max(...nums);
65+
const factors = Array.from({ length: maxNum + 1 }, () => 0);
66+
let result = 0;
67+
68+
const gcd = (a, b) => (b ? gcd(b, a % b) : a);
69+
70+
for (const num of nums) {
71+
for (let a = 1; a ** 2 <= num; a++) {
72+
if (num % a) continue;
73+
const b = num / a;
74+
75+
factors[a] = gcd(num, factors[a]);
76+
factors[b] = gcd(num, factors[b]);
77+
}
78+
}
79+
80+
for (let num = 1; num <= maxNum; num++) {
81+
if (num === factors[num]) {
82+
result += 1;
83+
}
84+
}
85+
86+
return result;
87+
};
88+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const countDifferentSubsequenceGCDs = function (nums) {
6+
const maxNum = Math.max(...nums);
7+
const factors = Array.from({ length: maxNum + 1 }, () => 0);
8+
let result = 0;
9+
10+
const gcd = (a, b) => (b ? gcd(b, a % b) : a);
11+
12+
for (const num of nums) {
13+
for (let a = 1; a ** 2 <= num; a++) {
14+
if (num % a) continue;
15+
const b = num / a;
16+
17+
factors[a] = gcd(num, factors[a]);
18+
factors[b] = gcd(num, factors[b]);
19+
}
20+
}
21+
22+
for (let num = 1; num <= maxNum; num++) {
23+
if (num === factors[num]) {
24+
result += 1;
25+
}
26+
}
27+
28+
return result;
29+
};

0 commit comments

Comments
 (0)