- Related Questions & Answers
- Count pairs of natural numbers with GCD equal to given number in C++
- Count number of triplets with product equal to given number in C++
- Count number of ways to partition a set into k subsets in C++
- Count number of triplets with product equal to given number with duplicates allowed in C++
- Count subarrays with equal number of occurrences of two given elements in C++
- Count number of sub-sequences with GCD 1 in C++
- Count number of subsets having a particular XOR value in C++
- Number of elements less than or equal to a given number in a given subarray in C++
- Print all subsets of given size of a set in C++
- Minimum number using set bits of a given number in C++
- Find all distinct subsets of a given set in C++
- Count number of right triangles possible with a given perimeter in C++
- Number of indexes with equal elements in given range in C++
- Python program to count number of vowels using set in a given string
- Count occurrences of the average of array elements with a given number in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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**

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

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

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 ]

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

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

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.

#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[0]); int size_GCD = sizeof(GCD)/sizeof(GCD[0]); subset_GCD(arr, size_arr, GCD, size_GCD); return 0; }

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

Advertisements