Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find number of different subsequences GCDs in Python
Suppose we have an array nums with positive values. We have to find the number of different GCDs among all non-empty subsequences of nums. As we know, the GCD of a sequence of numbers is the greatest value that divides all the numbers in the sequence evenly.
So, if the input is like nums = [4, 6, 18], then the output will be 4 because:
gcd([4]) = 4gcd([6]) = 6gcd([18]) = 18gcd([4, 6]) = 2gcd([4, 18]) = 2gcd([6, 18]) = 6gcd([4, 6, 18]) = 2
So all different GCD values are [4, 6, 18, 2], which gives us 4 distinct numbers.
Algorithm
To solve this, we will follow these steps:
-
T:= maximum of nums + 1 -
nums:= a new set containing all distinct numbers of nums -
ans:= 0 - For x in range 1 to T - 1, do:
-
g:= 0 - For y in range x to T - 1, update in each step by x, do:
- If y is in nums, then:
g:= gcd(g, y) - If g is same as x, then: break from loop
- If y is in nums, then:
- If g is same as x, then:
ans:= ans + 1
-
- Return ans
Example
Let us see the following implementation to get better understanding −
from math import gcd
def solve(nums):
T = max(nums) + 1
nums = set(nums)
ans = 0
for x in range(1, T):
g = 0
for y in range(x, T, x):
if y in nums:
g = gcd(g, y)
if g == x:
break
if g == x:
ans += 1
return ans
# Test the function
nums = [4, 6, 18]
result = solve(nums)
print(f"Number of different subsequence GCDs: {result}")
Number of different subsequence GCDs: 4
How It Works
The algorithm works by checking each possible GCD value from 1 to the maximum number in the array. For each candidate GCD x, it looks at all multiples of x that exist in the original array and computes their GCD. If the computed GCD equals x, then x is a valid subsequence GCD.
# Let's trace through the example step by step
from math import gcd
def solve_with_trace(nums):
T = max(nums) + 1
nums_set = set(nums)
ans = 0
print(f"Original array: {nums}")
print(f"Checking possible GCDs from 1 to {T-1}")
print()
for x in range(1, T):
g = 0
multiples = []
for y in range(x, T, x):
if y in nums_set:
multiples.append(y)
g = gcd(g, y)
if g == x:
break
if g == x:
ans += 1
print(f"GCD {x}: Found in multiples {multiples}, GCD = {g} ?")
else:
if multiples:
print(f"GCD {x}: Found in multiples {multiples}, GCD = {g} ?")
return ans
# Test with trace
nums = [4, 6, 18]
result = solve_with_trace(nums)
print(f"\nTotal different GCDs: {result}")
Original array: [4, 6, 18] Checking possible GCDs from 1 to 19 GCD 2: Found in multiples [2, 4, 6, 8, 10, 12, 14, 16, 18], GCD = 2 ? GCD 4: Found in multiples [4], GCD = 4 ? GCD 6: Found in multiples [6, 18], GCD = 6 ? GCD 18: Found in multiples [18], GCD = 18 ? Total different GCDs: 4
Conclusion
This algorithm efficiently finds all possible GCD values among subsequences by checking each potential GCD and verifying if it can be formed from the array elements. The time complexity is O(n * log(max(nums))) where n is the maximum value in the array.
