# Count number of sub-sequences with GCD 1

Given an array of N numbers, the task is to count the number of subsequences which 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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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

When we have values in 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 where consider two cases for every element, we either include or exclude it. We keep track of 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 either 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 memoization technique.

Below is the implementation of the above approach:

## C++

 `// C++ program to find the number ` `// of subsequences with gcd 1 ` `#include ` `using` `namespace` `std; ` `#define MAX 1000 ` `int` `gcd(``int` `a, ``int` `b) ` `{ ` `    ``if` `(a == 0) ` `        ``return` `b; ` `    ``return` `gcd(b % a, a); ` `} ` ` `  `// Recursive function to calculate the number ` `// of subsequences with gcd 1 starting with  ` `// particular index ` `int` `func(``int` `ind, ``int` `g, ``int` `dp[][MAX], ``int` `n, ``int` `a[]) ` `{ ` ` `  `    ``// Base case ` `    ``if` `(ind == n) { ` `        ``if` `(g == 1) ` `            ``return` `1; ` `        ``else` `            ``return` `0; ` `    ``} ` ` `  `    ``// If already visited ` `    ``if` `(dp[ind][g] != -1) ` `        ``return` `dp[ind][g]; ` ` `  `    ``// Either we take or we do not ` `    ``int` `ans = func(ind + 1, g, dp, n, a)  ` `              ``+ func(ind + 1, gcd(a[ind], g), dp, n, a); ` ` `  `    ``// Return the answer ` `    ``return` `dp[ind][g] = ans; ` `} ` ` `  `// Function to return the number of subsequences ` `int` `countSubsequences(``int` `a[], ``int` `n) ` `{ ` ` `  `    ``// Hash table to memoize ` `    ``int` `dp[n][MAX]; ` `    ``memset``(dp, -1, ``sizeof` `dp); ` ` `  `    ``// Count the number of subsequences ` `    ``int` `count = 0; ` ` `  `    ``// Count for every subsequence ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``count += func(i + 1, a[i], dp, n, a); ` ` `  `    ``return` `count; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `a[] = { 3, 4, 8, 16 }; ` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a[0]); ` `    ``cout << countSubsequences(a, n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find the number  ` `// of subsequences with gcd 1  ` `class` `GFG ` `{ ` `     `  `static` `final` `int` `MAX = ``1000``;  ` `static` `int` `gcd(``int` `a, ``int` `b)  ` `{  ` `    ``if` `(a == ``0``)  ` `        ``return` `b;  ` `    ``return` `gcd(b % a, a);  ` `}  ` ` `  `// Recursive function to calculate the number  ` `// of subsequences with gcd 1 starting with  ` `// particular index  ` `static` `int` `func(``int` `ind, ``int` `g, ``int` `dp[][], ` `                ``int` `n, ``int` `a[])  ` `{  ` ` `  `    ``// Base case  ` `    ``if` `(ind == n)  ` `    ``{  ` `        ``if` `(g == ``1``)  ` `            ``return` `1``;  ` `        ``else` `            ``return` `0``;  ` `    ``}  ` ` `  `    ``// If already visited  ` `    ``if` `(dp[ind][g] != -``1``)  ` `        ``return` `dp[ind][g];  ` ` `  `    ``// Either we take or we do not  ` `    ``int` `ans = func(ind + ``1``, g, dp, n, a)  ` `            ``+ func(ind + ``1``, gcd(a[ind], g), dp, n, a);  ` ` `  `    ``// Return the answer  ` `    ``return` `dp[ind][g] = ans;  ` `}  ` ` `  `// Function to return the ` `// number of subsequences  ` `static` `int` `countSubsequences(``int` `a[], ``int` `n)  ` `{  ` ` `  `    ``// Hash table to memoize  ` `    ``int` `dp[][] = ``new` `int``[n][MAX];  ` `    ``for``(``int` `i = ``0``; i < n; i++) ` `        ``for``(``int` `j = ``0``; j < MAX; j++) ` `            ``dp[i][j] = -``1``; ` ` `  `    ``// Count the number of subsequences  ` `    ``int` `count = ``0``;  ` ` `  `    ``// Count for every subsequence  ` `    ``for` `(``int` `i = ``0``; i < n; i++)  ` `        ``count += func(i + ``1``, a[i], dp, n, a);  ` ` `  `    ``return` `count;  ` `}  ` ` `  `// Driver Code  ` `public` `static` `void` `main(String args[]) ` `{  ` `    ``int` `a[] = { ``3``, ``4``, ``8``, ``16` `};  ` `    ``int` `n = a.length;  ` `    ``System.out.println(countSubsequences(a, n));  ` `} ` `}  ` ` `  `// This code is contributed by Arnab Kundu `

## Python3

 `# Python3 program to find the number  ` `# of subsequences with gcd 1  ` ` `  `MAX` `=` `1000` ` `  `def` `gcd(a, b):  ` `    ``if` `(a ``=``=` `0``): ` `        ``return` `b  ` `    ``return` `gcd(b ``%` `a, a) ` ` `  `# Recursive function to calculate the  ` `# number of subsequences with gcd 1  ` `# starting with particular index  ` `def` `func(ind, g, dp, n, a): ` ` `  `    ``# Base case  ` `    ``if` `(ind ``=``=` `n):  ` `        ``if` `(g ``=``=` `1``):  ` `            ``return` `1` `        ``else``: ` `            ``return` `0` ` `  `    ``# If already visited  ` `    ``if` `(dp[ind][g] !``=` `-``1``):  ` `        ``return` `dp[ind][g]  ` ` `  `    ``# Either we take or we do not  ` `    ``ans ``=` `(func(ind ``+` `1``, g, dp, n, a) ``+`  `           ``func(ind ``+` `1``, gcd(a[ind], g),  ` `                             ``dp, n, a))  ` ` `  `    ``# Return the answer  ` `    ``dp[ind][g] ``=` `ans ` `    ``return` `dp[ind][g] ` ` `  `# Function to return the number  ` `# of subsequences  ` `def` `countSubsequences(a, n):  ` ` `  `    ``# Hash table to memoize  ` `    ``dp ``=` `[[``-``1` `for` `i ``in` `range``(``MAX``)]  ` `              ``for` `i ``in` `range``(n)]  ` ` `  `    ``# Count the number of subsequences  ` `    ``count ``=` `0` ` `  `    ``# Count for every subsequence  ` `    ``for` `i ``in` `range``(n):  ` `        ``count ``+``=` `func(i ``+` `1``, a[i], dp, n, a)  ` ` `  `    ``return` `count  ` ` `  `# Driver Code  ` `a ``=` `[``3``, ``4``, ``8``, ``16` `]  ` `n ``=` `len``(a) ` `print``(countSubsequences(a, n))  ` ` `  `# This code is contributed by mohit kumar 29 `

## PHP

 ` `

Output:

```7
```

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

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.