Skip to content

Commit 209fcdd

Browse files
committed
New Problem Solution -"1819. Number of Different Subsequences GCDs"
1 parent 984ca35 commit 209fcdd

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ LeetCode
99

1010
| # | Title | Solution | Difficulty |
1111
|---| ----- | -------- | ---------- |
12+
|1819|[Number of Different Subsequences GCDs](https://leetcode.com/problems/number-of-different-subsequences-gcds/) | [C++](./algorithms/cpp/numberOfDifferentSubsequencesGcds/NumberOfDifferentSubsequencesGcds.cpp)|Hard|
1213
|1818|[Minimum Absolute Sum Difference](https://leetcode.com/problems/minimum-absolute-sum-difference/) | [C++](./algorithms/cpp/minimumAbsoluteSumDifference/MinimumAbsoluteSumDifference.cpp)|Medium|
1314
|1817|[Finding the Users Active Minutes](https://leetcode.com/problems/finding-the-users-active-minutes/) | [C++](./algorithms/cpp/findingTheUsersActiveMinutes/FindingTheUsersActiveMinutes.cpp)|Medium|
1415
|1816|[Truncate Sentence](https://leetcode.com/problems/truncate-sentence/) | [C++](./algorithms/cpp/truncateSentence/TruncateSentence.cpp)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Source : https://leetcode.com/problems/number-of-different-subsequences-gcds/
2+
// Author : Hao Chen
3+
// Date : 2021-04-05
4+
5+
/*****************************************************************************************************
6+
*
7+
* You are given an array nums that consists of positive integers.
8+
*
9+
* The GCD of a sequence of numbers is defined as the greatest integer that divides all the numbers in
10+
* the sequence evenly.
11+
*
12+
* For example, the GCD of the sequence [4,6,16] is 2.
13+
*
14+
* A subsequence of an array is a sequence that can be formed by removing some elements (possibly
15+
* none) of the array.
16+
*
17+
* For example, [2,5,10] is a subsequence of [1,2,1,2,4,1,5,10].
18+
*
19+
* Return the number of different GCDs among all non-empty subsequences of nums.
20+
*
21+
* Example 1:
22+
*
23+
* Input: nums = [6,10,3]
24+
* Output: 5
25+
* Explanation: The figure shows all the non-empty subsequences and their GCDs.
26+
* The different GCDs are 6, 10, 3, 2, and 1.
27+
*
28+
* Example 2:
29+
*
30+
* Input: nums = [5,15,40,5,6]
31+
* Output: 7
32+
*
33+
* Constraints:
34+
*
35+
* 1 <= nums.length <= 10^5
36+
* 1 <= nums[i] <= 2 * 10^5
37+
******************************************************************************************************/
38+
39+
class Solution {
40+
private:
41+
// Euclidean algorithm
42+
// https://en.wikipedia.org/wiki/Euclidean_algorithm
43+
int gcd(int a, int b) {
44+
while ( b != 0 ) {
45+
int t = b;
46+
b = a % b;
47+
a = t;
48+
}
49+
return a;
50+
}
51+
52+
public:
53+
int countDifferentSubsequenceGCDs(vector<int>& nums) {
54+
int len = nums.size();
55+
vector<int> gcds(200001, 0);
56+
57+
for(int i=0; i<len; i++) {
58+
int n = nums[i];
59+
int m = sqrt(n);
60+
for(int g=1; g<=m; g++){
61+
if (n % g != 0) continue;
62+
int x = g, y = n/g;
63+
if (x != y ){
64+
gcds[x] = gcd(n, gcds[x]);
65+
gcds[y] = gcd(n, gcds[y]);
66+
}else {
67+
gcds[x] = gcd(n, gcds[x]);
68+
}
69+
}
70+
}
71+
72+
int cnt = 0;
73+
for(int i=1; i<gcds.size(); i++){
74+
if (gcds[i]==i) cnt++;
75+
}
76+
return cnt;
77+
}
78+
};

0 commit comments

Comments
 (0)