You can take a string with k no. of 1's at the end and generate all
possible permutations of the string thus formed.Doing this way you can
generate all possible strings and corresponding to each "1" in string
write the number in array(corresponding to that position) and thus you
can generate all subsets of size k.Here is my code:

#include<iostream>
#include<algorithm>
#include<string>

using namespace std;

int main()
{
        int a[1000];
        int n;
        int k;
        char s[1000];
        cin>>n>>k;
        int count_z=n-k;
        for(int i=0;i<count_z;i++)
                s[i]='0';
        for(int i=count_z;i<n;i++)
                s[i]='1';
        s[n]='\0';
        for(int i=0;i<n;i++)
                cin>>a[i];
        do{
                for(int i=0;i<strlen(s);i++){
                        if(s[i]=='1'){
                                cout<<a[i]<<" ";
                        }
                }
                cout<<"\n";
        }while(next_permutation(s,s+strlen(s)));

        return 0;
}

Hope this will help....

On Jul 25, 11:29 pm, sumit <sumitispar...@gmail.com> wrote:
> Given an array of size n, print all the possible subset of array of
> size k.
> eg:-
> input:
> a[]={1,2,3,4}, k = 2
> output:
> 1,2
> 1,3
> 1,4
> 2,3
> 2,4
> 3,4

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to