-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSherlockArray.c
60 lines (55 loc) · 1.14 KB
/
SherlockArray.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
/* This is a brute force approach to solve the problem with a runtime of O(n).
Find if array can be divided into two subarrays of equal sum
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int findSum(int a[], int len)
{
if (len==1)
return 1;
int i = 1;
int sumL = summer(a,0,i-1);
int sumR = summer (a, i+1,len-1 );
while (i<= len-2)
{
if (sumL == sumR)
return 1;
sumL += a[i];
sumR -= a[i+1];
i++;
}
return 0;
}
int summer(int a[], int s, int e)
{
int sum = 0;
for (int i=s; i<=e; i++)
sum+= a[i];
//printf("%d\n", sum);
return sum;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int tc;
scanf("%d", &tc);
int i=0;
for (i=0; i<tc; i++)
{
int len;
scanf("%d", &len);
int j=0;
int a[len];
for (j=0; j<len; j++)
{
scanf("%d", &a[j]);
}
int res = findSum(a,len);
if (res==1)
printf("%s", "YES\n");
else
printf("%s", "NO\n");
}
return 0;
}