forked from Adityaranjanpatra/Btecky2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
harshPr4
88 lines (79 loc) · 1.69 KB
/
harshPr4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<iostream>
using namespace std;
// A function to sort the data set.
void Sort(int a[], int n)
{
int i, j, temp;
for(i = 0; i < n; i++)
{
for(j = i+1; j < n; j++)
{
if(a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
// A function to print all combination of a given length from the given array.
void GenSubset(int a[], int reqLen, int start, int currLen, bool check[], int len)
{
// Return if the currLen is more than the required length.
if(currLen > reqLen)
return;
// If currLen is equal to required length then print the sequence.
else if (currLen == reqLen)
{
cout<<"\t";
cout<<"{ ";
for (int i = 0; i < len; i++)
{
if (check[i] == true)
{
cout<<a[i]<<" ";
}
}
cout<<"}\n";
return;
}
// If start equals to len then return since no further element left.
if (start == len)
{
return;
}
// For every index we have two options.
// First is, we select it, means put true in check[] and increment currLen and start.
check[start] = true;
GenSubset(a, reqLen, start + 1, currLen + 1, check, len);
// Second is, we don't select it, means put false in check[] and only start incremented.
check[start] = false;
GenSubset(a, reqLen, start + 1, currLen, check, len);
}
}
int main()
{
int i, n;
bool check[n];
cout<<"Enter the number of element array have: ";
cin>>n;
int arr[n];
cout<<"\n";
// Take the input of the array.
for(i = 0; i < n; i++)
{
cout<<"Enter "<<i+1<<" element: ";
cin>>arr[i];
check[i] = false;
}
// Sort the array.
Sort(arr, n);
cout<<"\nThe subset in the lexicographic order: \n";
cout<<"\t{ }\n";
for(i = 1; i <= n; i++)
{
GenSubset(arr, i, 0, 0, check, n);
}
return 0;
}