LeetCode Problem Workspace

Find the Number of Subsequences With Equal GCD

Count all pairs of non-empty subsequences in an integer array whose elements share the same greatest common divisor efficiently using DP.

category

4

Topics

code_blocks

0

Code langs

hub

3

Related

Practice Focus

Hard · State transition dynamic programming

bolt

Answer-first summary

Count all pairs of non-empty subsequences in an integer array whose elements share the same greatest common divisor efficiently using DP.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for State transition dynamic programming

Try AiBox Copilotarrow_forward

This problem requires counting pairs of subsequences where each pair shares an identical GCD. A state transition dynamic programming approach efficiently tracks counts of subsequences up to each index with specific GCDs. By iterating through array elements and updating DP states, you avoid redundant GCD computations and handle large input arrays within constraints.

Problem Statement

Given an integer array nums, determine how many pairs of non-empty subsequences exist such that the GCD of elements in the first subsequence equals the GCD of elements in the second subsequence. Each pair (seq1, seq2) must be counted only once regardless of order.

Return the total number of valid subsequence pairs. For example, nums = [1,2,3,4] has 10 pairs, while nums = [10,20,30] has 2 pairs. Constraints: 1 <= nums.length <= 200, 1 <= nums[i] <= 200.

Examples

Example 1

Input: nums = [1,2,3,4]

Output: 10

The subsequence pairs which have the GCD of their elements equal to 1 are:

Example 2

Input: nums = [10,20,30]

Output: 2

The subsequence pairs which have the GCD of their elements equal to 10 are:

Example 3

Input: nums = [1,1,1,1]

Output: 50

Example details omitted.

Constraints

  • 1 <= nums.length <= 200
  • 1 <= nums[i] <= 200

Solution Approach

State Transition DP

Use a 2D DP array where dp[i][g] represents the count of subsequences ending at index i with GCD g. Iterate through nums and for each element, combine it with previous subsequences to update GCD counts efficiently.

Efficient GCD Updates

Instead of recomputing GCDs for all subsequences, update existing DP states using gcd(prevGCD, currentNum). This avoids redundant calculations and directly maintains the number of subsequences per GCD.

Final Pair Counting

Once all subsequences and their GCDs are tracked, iterate through DP counts to calculate the number of valid pairs. Multiply counts of subsequences sharing the same GCD to derive the final answer.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity depends on updating DP states for each number and GCD combination, roughly O(n maxVal^2). Space complexity is determined by storing counts for each possible GCD, O(n maxVal), where maxVal is the largest number in nums.

What Interviewers Usually Probe

  • Check if the candidate correctly identifies the DP state representing subsequences and their GCDs.
  • Observe whether they optimize GCD updates instead of recomputing for all subsequences.
  • Look for awareness of subsequence pair counting and avoiding double counting.

Common Pitfalls or Variants

Common pitfalls

  • Recomputing GCD for every subsequence instead of updating via DP.
  • Counting subsequences incorrectly and including empty subsequences.
  • Double counting pairs where order does not matter.

Follow-up variants

  • Count subsequences with equal LCM instead of GCD.
  • Find pairs of subsequences where the sum is equal rather than GCD.
  • Limit subsequences to length k and count pairs with equal GCD.

FAQ

What is the main pattern used in Find the Number of Subsequences With Equal GCD?

The problem relies on state transition dynamic programming to track counts of subsequences with specific GCDs efficiently.

How do I avoid double counting subsequence pairs?

Ensure each pair is counted only once by multiplying counts from DP states and avoiding permutations of the same pair.

Can this DP approach handle large numbers up to 200?

Yes, because DP tracks counts per GCD and number, limiting operations to manageable O(n*maxVal^2).

Is it necessary to consider empty subsequences?

No, only non-empty subsequences are counted when forming valid pairs.

How does GhostInterview suggest handling GCD updates?

It recommends updating existing DP states using gcd(prevGCD, currentNum) instead of recomputing for each subsequence.

terminal

Solution

Solution 1

#### Python3

1
Find the Number of Subsequences With Equal GCD Solution: State transition dynamic programming | LeetCode #3336 Hard