File tree 1 file changed +64
-0
lines changed
1 file changed +64
-0
lines changed Original file line number Diff line number Diff line change
1
+ //
2
+ // main.cpp
3
+ // subset
4
+ //
5
+ // Created by Prince Kumar on 09/09/19.
6
+ // Copyright © 2019 Prince Kumar. All rights reserved.
7
+ //
8
+ // Print all subset of size 'k' of array of size 'n'
9
+
10
+ #include < iostream>
11
+ #include < vector>
12
+ #include < math.h>
13
+ #include < algorithm>
14
+ using namespace std ;
15
+ int main ()
16
+ {
17
+ cout<<" Enter number of elemnets in array : " <<endl;
18
+ int n; cin>>n;
19
+ cout<<" Enter the size of subset : " <<endl;
20
+ int k ; cin>>k;
21
+
22
+ int a[n];
23
+ cout<<" Enter the numbers : " <<endl;
24
+ for (int i=0 ;i<n;i++)
25
+ {
26
+ cin>>a[i];
27
+ }
28
+ for (int i=1 ;i<pow (2 ,n);i++)
29
+ {
30
+ vector<int > v;
31
+ int x=i; // x=8
32
+ while (x>0 )
33
+ {
34
+ int z = x%2 ; // 0 0 0 1
35
+ v.push_back (z);
36
+ x=x/2 ; // 4 2 1 0
37
+ }
38
+ int p = n- (int )v.size ();
39
+ for (int j=1 ;j<=p;j++)
40
+ {
41
+ v.push_back (0 );
42
+ }
43
+ reverse (v.begin (), v.end ());
44
+ int count=0 ;
45
+ for (int i=0 ;i<v.size ();i++)
46
+ {
47
+ if (v[i]==1 )
48
+ count++;
49
+ }
50
+ if (count==k)
51
+ {
52
+ for (int i=0 ;i<v.size ();i++)
53
+ {
54
+ if (v[i]==1 )
55
+ {
56
+ cout<<a[i]<<" " ;
57
+ }
58
+ }
59
+ cout<<endl;
60
+ }
61
+ v.clear ();
62
+
63
+ }
64
+ }
You can’t perform that action at this time.
0 commit comments