dimanche 15 septembre 2019

Print all subsets of given size of a set and count the subset

Generate all possible subset of size r of given array with distinct elements. i need help for counting the subset after getting all possible subset of size r of given array with distinct elements. how to count the subset of distinct element after generated

    #include <bits/stdc++.h> 
    using namespace std;
    void combinationUtil(int arr[], int n, int r, int index, int data[], int i); 

    void printCombination(int arr[], int n, int r) 
    { 
        int data[r]; 

        combinationUtil(arr, n, r, 0, data, 0); 
    } 
    void combinationUtil(int arr[], int n, int r, int index,int data[], int i) 
    { 
        int c=0; 
        if (index == r) { 
            for (int j = 0; j < r; j++) {
                printf("%d ", data[j]); 
            }
            printf("\n"); 
            return; 
        } 

        if (i >= n) 
            return; 

        data[index] = arr[i]; 
        combinationUtil(arr, n, r, index + 1, data, i + 1); 
        combinationUtil(arr, n, r, index, data, i + 1); 
    } 

    int main() 
    { 
        int arr[] = { 1,2,3,4}; 
        int r = 2; 
        int n = sizeof(arr) / sizeof(arr[0]); 
        printCombination(arr, n, r); 
        return 0; 
    }

output 
0 1
0 2
0 3
0 4
1 2
1 3
1 4
2 3
2 4
3 4
number of subset 10

Aucun commentaire:

Enregistrer un commentaire