-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrint all subsets of given size of a set.c
78 lines (62 loc) · 1.37 KB
/
Print all subsets of given size of a set.c
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
/*
// Sample code to perform I/O:
#include <stdio.h>
int main(){
int num;
scanf("%d", &num); // Reading input from STDIN
printf("Input number is %d.\n", num); // Writing output to STDOUT
}
// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
// Write your code here
-8 0 -2
-8 =1
0 =1
-2 =1
-8 0 =2
-8 -2 =2
0 -2 =2
-8 0 -2 =3
*/
#include<stdio.h>
int print(int *a,int *b,int index,int i,int k,int sum,int n)
{
if(index==k)
{
int j;
for(j=0;j<k;j++)
{
printf("%d\n",b[j]);
sum=sum+k*b[j];
}
return sum;
}
if(i>=n)
return sum;
b[index]=a[i];
sum=print(a,b,index+1,i+1,k,sum,n);
sum=print(a,b,index,i+1,k,sum,n);
}
void main()
{
int t;
int i,j;
scanf("%d",&t);
for(j=1;j<=t;j++)
{
int n,sum=0,s=-10000;
scanf("%d",&n);
long int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n;i++)
{
int b[i];
sum=print(a,b,0,0,i,0,n);
if(sum>s)
{
s=sum;
}
}
printf("%d",s);
}
}