-3

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[] = { 0,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
walnut
  • 21,629
  • 4
  • 23
  • 59
  • 1
    1) `int data[r];` -- This is not valid C++. 2) `#include ` -- Use the proper headers, not this one. 3) This problem is solved by using `std::next_permutation` along with a few other items, all without recursion. – PaulMcKenzie Sep 15 '19 at 18:31
  • how about of size of an array and k length – chitaranjan pradhan Sep 15 '19 at 18:36
  • [Possible duplicate](https://stackoverflow.com/questions/9430568/generating-combinations-in-c) – PaulMcKenzie Sep 15 '19 at 18:37
  • 1
    Possible duplicate of [Generating combinations in c++](https://stackoverflow.com/questions/9430568/generating-combinations-in-c) – S.S. Anne Oct 12 '19 at 13:03

1 Answers1

0

Let's solve this with: next_permutation. This will permute an input, returning false when all permutations have been visited.
Given the sorted input: int arr[] we can do:

do {
    copy(cbegin(arr), cend(arr), ostream_iterator<int>(cout, " "));
    cout << endl;
} while(next_permutation(begin(arr), end(arr)));

Live Example

This example assumes unique inputs, in which case a combination and permutation are the same. If you don't have unique inputs you're really asking for a combination of these numbers. next_combination has been purposed here: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2639.pdf You can copy that implementation if you find that you won't have unique inputs and just use that. You can also find out more about next_combination here: https://stackoverflow.com/a/35215540/2642059

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • Yes, but I think the OP wanted combinations. You still use `next_permutation`, but the permutation will be on the boolean array that points to certain values in `arr`. – PaulMcKenzie Sep 15 '19 at 18:50
  • @chitaranjanpradhan So it looks like as has been stated in the comments that you are looking for `next_combination` not `next_permutation`. Sadly this hasn't been accepted into the standard yet. When I've needed it I've just copied the implementation out of that proposal and used it. – Jonathan Mee Sep 15 '19 at 19:08