Number of Different Subsequences GCDs - Problem
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]is2.
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.
Input & Output
Example 1 — Small Array
$
Input:
nums = [6,10,3]
›
Output:
5
💡 Note:
All possible subsequences and their GCDs: [6]→6, [10]→10, [3]→3, [6,10]→2, [6,3]→3, [10,3]→1, [6,10,3]→1. Unique GCDs: {1,2,3,6,10}, so answer is 5.
Example 2 — Identical Elements
$
Input:
nums = [5,15,40,5,6]
›
Output:
7
💡 Note:
With elements 5,15,40,5,6, we can form subsequences with GCDs: 1,2,3,5,6,15,40. Total unique GCDs = 7.
Example 3 — Single Element
$
Input:
nums = [42]
›
Output:
1
💡 Note:
Only one subsequence [42] with GCD = 42. Answer is 1.
Constraints
- 1 ≤ nums.length ≤ 103
- 1 ≤ nums[i] ≤ 2 × 105
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array [6, 10, 3] with positive integers
2
Process
Find all possible subsequence GCDs efficiently
3
Output
Count of unique GCDs: 5
Key Takeaway
🎯 Key Insight: Rather than generating exponentially many subsequences, test each potential GCD by collecting its multiples and verifying their actual GCD
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code