forked from chitwang/iitk-sem1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group-sum.c
73 lines (56 loc) · 1.21 KB
/
group-sum.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
/*Given an array of N integers, is it possible to choose a group of some of the integers, such that the group sums to the given target T.
Input will consist of two lines : space separated integers N and T in first line followed by N space separated integer elements (a1 ... aN ) of array in second line.
Output should be single line - either "YES" or "NO".
Constraints:
0 <= N <= 15
Sample Input:
4 2
3 5 7 1
Sample Output:
NO
Public test cases:
Input
4 2
3 5 7 1
output:
NO*/
// solution:
#include <stdio.h>
void recursion(int arr[], int in, int n, int s, int istart, int sub[], int j, int *flag)
{
if (in == s)
{
*flag = *flag + 1;
return;
}
if (in > s)
{
return;
}
for (int i = istart; i < n; i++)
{
istart = i;
sub[j] = arr[istart];
recursion(arr, in + arr[i], n, s, istart + 1, sub, j + 1, flag);
}
return;
}
int main()
{
int n;
int s;
scanf("%d", &n);
scanf("%d", &s);
int arr[n];
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
int sub[n];
int flag = 0;
recursion(arr, 0, n, s, 0, sub, 0, &flag);
if (!flag)
printf("NO");
else
printf("YES");
}