Solving Leetcode Interviews in Seconds with AI: Find the Number of Subsequences With Equal GCD
Introduction
In this blog post, we will explore how to solve the LeetCode problem "3336" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
You are given an integer array nums. Your task is to find the number of pairs of non-empty subsequences (seq1, seq2) of nums that satisfy the following conditions: The subsequences seq1 and seq2 are disjoint, meaning no index of nums is common between them. The GCD of the elements of seq1 is equal to the GCD of the elements of seq2. Return the total number of such pairs. Since the answer may be very large, return it modulo 109 + 7. Example 1: Input: nums = [1,2,3,4] Output: 10 Explanation: The subsequence pairs which have the GCD of their elements equal to 1 are: ([1, 2, 3, 4], [1, 2, 3, 4]) ([1, 2, 3, 4], [1, 2, 3, 4]) ([1, 2, 3, 4], [1, 2, 3, 4]) ([1, 2, 3, 4], [1, 2, 3, 4]) ([1, 2, 3, 4], [1, 2, 3, 4]) ([1, 2, 3, 4], [1, 2, 3, 4]) ([1, 2, 3, 4], [1, 2, 3, 4]) ([1, 2, 3, 4], [1, 2, 3, 4]) ([1, 2, 3, 4], [1, 2, 3, 4]) ([1, 2, 3, 4], [1, 2, 3, 4]) Example 2: Input: nums = [10,20,30] Output: 2 Explanation: The subsequence pairs which have the GCD of their elements equal to 10 are: ([10, 20, 30], [10, 20, 30]) ([10, 20, 30], [10, 20, 30]) Example 3: Input: nums = [1,1,1,1] Output: 50 Constraints: 1 <= nums.length <= 200 1 <= nums[i] <= 200
Explanation
Here's the breakdown of the solution:
- Count occurrences of each GCD: Iterate through all possible GCDs from 1 to the maximum element in
nums. For each potential GCD, count how many subsequences have that GCD. This is done efficiently by considering only numbers innumsthat are divisible by the current GCD. - Calculate number of disjoint pairs: For each GCD, if we have
countsubsequences with that GCD, the number of disjoint pairs iscount * count. Modulo arithmetic: Apply the modulo operator (
% (10**9 + 7)) at each step to prevent overflow.Runtime Complexity: O(N M log(M)), where N is the length of
numsand M is the maximum element innums. The log(M) factor comes from the gcd calculation.- Storage Complexity: O(M)
Code
from math import gcd
def count_disjoint_subsequences(nums):
"""
Finds the number of pairs of non-empty disjoint subsequences of nums
with equal GCDs.
Args:
nums: A list of integers.
Returns:
The number of pairs of disjoint subsequences with equal GCDs, modulo 10^9 + 7.
"""
MOD = 10**9 + 7
max_num = max(nums)
counts = [0] * (max_num + 1)
for i in range(1, max_num + 1):
subset = [num for num in nums if num % i == 0]
n = len(subset)
if n == 0:
continue
counts[i] = pow(2, n, MOD) - 1
counts[i] %= MOD
for j in range(2 * i, max_num + 1, i):
counts[i] -= counts[j]
counts[i] %= MOD
result = 0
for count in counts:
result += (count * count) % MOD
result %= MOD
return result