Number of Different Subsequences GCDs
HardLeetCodeNumber Theory
Approach hintTry it first, then revealHide
Do not enumerate subsequences. For each candidate g up to the maximum value, take the gcd of all present multiples of g; g is achievable exactly when that gcd equals g. Harmonic-sum time O(M log M).