-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkpartition.cpp
More file actions
55 lines (51 loc) · 814 Bytes
/
kpartition.cpp
File metadata and controls
55 lines (51 loc) · 814 Bytes
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
#include <bits/stdc++.h>
using namespace std;
void print(string t)
{
cout<<t<<"\n";
}
void kpartition(int *a,int n,int k,string t="",int pos=0,int l=0)
{
if(pos==n&&l==k)
{
print(t);
return;
}
for(int i=pos;i<n;i++)
{
string p;
for(int k=pos;k<=i;k++)
{
p+=to_string(a[k]);
p+=" ";
}
kpartition(a,n,k,t+"("+p+")",i+1,l+1);
}
}
signed main()
{
int *a,n;
int *b;
cout<<"n?"<<"\n";
cin>>n;
a=new int[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<"\n";
int k;
cout<<"enter number of partitions to divide\n";
cin>>k;
if(k>n)
{
printf("not possible to partition\n");
return -1;
}
kpartition(a,n,k);
return 0;
}