VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-number-of-sub-sequences-with-gcd-1/

⇱ Count number of sub-sequences with GCD 1 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count number of sub-sequences with GCD 1

Last Updated : 11 Jul, 2025

Given an array of N numbers, the task is to count the number of subsequences that have gcd equal to 1. 

Examples: 

Input: a[] = {3, 4, 8, 16} 
Output: 7
The subsequences are: 
{3, 4}, {3, 8}, {3, 16}, {3, 4, 8},
{3, 4, 16}, {3, 8, 16}, {3, 4, 8, 16}

Input: a[] = {1, 2, 4}
Output: 4

A simple solution is to generate all subsequences or subsets. For every subsequence, check if its GCD is 1 or not. If 1, increment the result.

When we have values in the array (say all smaller than 1000), we can optimize the above solution as we know that number of possible GCDs would be small. We modify the recursive subset generation algorithm was considering two cases for every element, we either include or exclude it. We keep track of the current GCD and if we have already counted for this GCD, we return the count. So when we are considering a subset, some GCDs would appear again and again. Therefore the problem can be solved using Dynamic Programming. Given below are the steps to solve the above problem: 

  • Start from every index and call the recursive function by taking the index element.
  • In the recursive function, we iterate till we reach N.
  • The two recursive calls will be based on whether we take the index element or not.
  • The base case will be to return 1 if we have reached the end and the gcd till now is 1.
  • Two recursive calls will be func(ind+1, gcd(a[i], prevgcd)) and func(ind+1, prevgcd)
  • The overlapping subproblems can be avoided by using the memoization technique.

Below is the implementation of the above approach:  


Output
7

Complexity Analysis:

  • Time Complexity: O(logN*N2), as we are recursively calling the function two times but we are using memorization and the GCD function will cost O(logN). Where N is the number of elements in the array.
  • Auxiliary Space: O(N*1000), as we are using extra space for the DP matrix. Where N is the number of elements in the array.

Alternate Solution: Count the number of subsets of a set with GCD equal to a given number

Dynamic programming approach to this problem without memoization:

Basically, the approach will be making a 2d matrix in which i coordinate will be the position of elements of the given array and the j coordinate will be numbers from 0 to 100 ie. gcd can vary from 0 to 100 if array elements are not enough large.  we will iterate on the given array and the 2d matrix will store information that till ith position that how many subsequences are there having gcd vary from 1 to 100. later on, we will add dp[i][1] to get all subsequences having gcd as 1.

Below is the implementation of the above approach:


Output
7

Complexity Analysis:

  • Time Complexity: O(100*logN*N2), as we are using nested loops and the GCD function will cost O(logN). Where N is the number of elements in the array.
  • Auxiliary Space: O(N*100), as we are using extra space for the DP matrix. Where N is the number of elements in the array.
Comment
Article Tags: