Skip to content

Commit

Permalink
New Problem Solution -"1819. Number of Different Subsequences GCDs"
Browse files Browse the repository at this point in the history
  • Loading branch information
haoel committed Apr 5, 2021
1 parent 984ca35 commit 209fcdd
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ LeetCode

| # | Title | Solution | Difficulty |
|---| ----- | -------- | ---------- |
|1819|[Number of Different Subsequences GCDs](https://leetcode.com/problems/number-of-different-subsequences-gcds/) | [C++](./algorithms/cpp/numberOfDifferentSubsequencesGcds/NumberOfDifferentSubsequencesGcds.cpp)|Hard|
|1818|[Minimum Absolute Sum Difference](https://leetcode.com/problems/minimum-absolute-sum-difference/) | [C++](./algorithms/cpp/minimumAbsoluteSumDifference/MinimumAbsoluteSumDifference.cpp)|Medium|
|1817|[Finding the Users Active Minutes](https://leetcode.com/problems/finding-the-users-active-minutes/) | [C++](./algorithms/cpp/findingTheUsersActiveMinutes/FindingTheUsersActiveMinutes.cpp)|Medium|
|1816|[Truncate Sentence](https://leetcode.com/problems/truncate-sentence/) | [C++](./algorithms/cpp/truncateSentence/TruncateSentence.cpp)|Easy|
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Source : https://leetcode.com/problems/number-of-different-subsequences-gcds/
// Author : Hao Chen
// Date : 2021-04-05

/*****************************************************************************************************
*
* You are given an array nums that consists of positive integers.
*
* The GCD of a sequence of numbers is defined as the greatest integer that divides all the numbers in
* the sequence evenly.
*
* For example, the GCD of the sequence [4,6,16] is 2.
*
* A subsequence of an array is a sequence that can be formed by removing some elements (possibly
* none) of the array.
*
* For example, [2,5,10] is a subsequence of [1,2,1,2,4,1,5,10].
*
* Return the number of different GCDs among all non-empty subsequences of nums.
*
* Example 1:
*
* Input: nums = [6,10,3]
* Output: 5
* Explanation: The figure shows all the non-empty subsequences and their GCDs.
* The different GCDs are 6, 10, 3, 2, and 1.
*
* Example 2:
*
* Input: nums = [5,15,40,5,6]
* Output: 7
*
* Constraints:
*
* 1 <= nums.length <= 10^5
* 1 <= nums[i] <= 2 * 10^5
******************************************************************************************************/

class Solution {
private:
// Euclidean algorithm
// https://en.wikipedia.org/wiki/Euclidean_algorithm
int gcd(int a, int b) {
while ( b != 0 ) {
int t = b;
b = a % b;
a = t;
}
return a;
}

public:
int countDifferentSubsequenceGCDs(vector<int>& nums) {
int len = nums.size();
vector<int> gcds(200001, 0);

for(int i=0; i<len; i++) {
int n = nums[i];
int m = sqrt(n);
for(int g=1; g<=m; g++){
if (n % g != 0) continue;
int x = g, y = n/g;
if (x != y ){
gcds[x] = gcd(n, gcds[x]);
gcds[y] = gcd(n, gcds[y]);
}else {
gcds[x] = gcd(n, gcds[x]);
}
}
}

int cnt = 0;
for(int i=1; i<gcds.size(); i++){
if (gcds[i]==i) cnt++;
}
return cnt;
}
};

0 comments on commit 209fcdd

Please sign in to comment.