# Count number of subsets of a set with GCD equal to a given number in C++

Given an array ar, containing positive numbers and an array GCD[] containing gcd values.The goal is to find the number of subsets of elements of arr[] that have gcd values as given in GCD[].

For Example

## Input

arr[] = {10, 5, 6, 3}, GCD[] = {2, 3, 5}

## Output

Count of number of subsets of a set with GCD equal to a given number are: 1 2 2

## Explanation

The subsets with GCD equal to 2 is [ 10, 6 ].
Subsets with GCD equal to 3 is [ 3 ], [ 6,3 ]
Subsets with GCD equal to 5 is [ 5 ], [ 10, 5 ]

## Input

arr[] = {10, 21, 7, 8}, GCD[] = {2, 7, 5}

## Output

Count of number of subsets of a set with GCD equal to a given number are: 1
2 0

## Explanation

The subsets with GCD equal to 2 is [ 10, 8 ].
Subsets with GCD equal to 7 is [ 7 ], [ 21,7 ]
There are no subsets with GCD equal to 5.

Approach used in the below program is as follows

In this approach we will make an unordered_map<int, int> um_1 for storing the frequencies of elements of arr[] and similar map um_2 for storing the number of subsets with given gcd. Take the maximum value of among elements of arr[] as count. Now run a loop from i=count to i>=1 and find the number of subsets for current gcd. For this we will count the number of multiples of i in um_1. If the number of multiples of i is total then the number of subsets with gcd i is total2−1−temp. Where temp is the number of subsets that have gcd greater than i but not equal to i.

• Take two arrays for arr[] and GCD[].

• Function subset_GCD(int arr[], int size_arr, int GCD[], int size_GCD) takes both arrays and their lengths and returns the count of the number of subsets of a set with GCD equal to a given number.

• Function subset_GCD(int arr[], int size_arr, int GCD[], int size_GCD) takes both arrays and their lengths and returns the count of the number of subsets of a set with GCD equal to a given number.

• Take the initial count as 0.

• Traverse arr[] using for loop and find update count as maximum value and update um_1 with frequencies using um_1[arr[i]]++.

• Using a for loop from i=count to i>=1, take the total as the sum of frequencies of multiples of i and temp=0 as the number of subsets that have gcd greater than i but not equal to i.

• Traverse again from j=2 to j*i<=count, add um_1[j*i] to total and add um_2[j*i] to temp.

• After the end of both for loops set um_2[i] = (1<<total) − 1 − temp.

• Print um_2[GCD[i]] for resultant array that have count of subsets with GCD given.

## Example

Live Demo

#include<bits/stdc++.h>
using namespace std;
void subset_GCD(int arr[], int size_arr, int GCD[], int size_GCD){
unordered_map<int, int> um_1, um_2;
int count = 0;
for (int i=0; i<size_arr; i++){
count = max(count, arr[i]);
um_1[arr[i]]++;
}
for (int i = count; i >=1; i−−){
int temp = 0;
int total = um_1[i];
for (int j = 2; j*i <= count; j++){
total += um_1[j*i];
temp += um_2[j*i];
}
um_2[i] = (1<<total) − 1 − temp;
}
cout<<"Count of number of subsets of a set with GCD equal to a given number are: ";
for (int i=0; i<size_GCD ; i++){
cout<<um_2[GCD[i]]<<" ";
}
}
int main(){
int GCD[] = {2, 3};
int arr[] = {9, 6, 2};
int size_arr = sizeof(arr)/sizeof(arr);
int size_GCD = sizeof(GCD)/sizeof(GCD);
subset_GCD(arr, size_arr, GCD, size_GCD);
return 0;
}

## Output

If we run the above code it will generate the following output −

Count of number of subsets of a set with GCD equal to a given number are: 2 1