-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrisectArray.c
More file actions
85 lines (81 loc) · 1.2 KB
/
TrisectArray.c
File metadata and controls
85 lines (81 loc) · 1.2 KB
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
//Solving TA problem using sum array and PnC
#include <stdio.h>
long long sum[1000009],arr[1000009],ind[3][1000009];
int main()
{
long long i,j,k,h,n,count1=0,count2=0,count=0,total,compare;
scanf("%lld",&n);
for(i=0;i<n;i++)
{
scanf("%lld",&arr[i]);
if(i)
sum[i]=sum[i-1]+arr[i];
else
sum[0]=arr[0];
// printf("%lld ",sum[i]);
}
total=sum[n-1];
compare=total/3;
//printf("\n%lld\n",total);
h=0;
for(i=0;i<n-2;i++)
{
if(sum[i]==compare)
{
ind[0][h]=i;;
count1++;
h++;
}
//if(sum[i]==2*compare)
// index[i]=2;
}
/*for(i=0;i<h;i++)
printf("%lld ",ind[0][i]);
printf("\n");*/
k=0;
for(i=0;i<n-1;i++)
{
if(sum[i]==2*compare)
{
ind[1][k]=i;
count2++;
k++;
}
}
/*for(i=0;i<k;i++)
printf("%lld ",ind[1][i]);
printf("\n");*/
/*for(i=0;i<h;i++)
for(j=0;j<k;j++)
if(ind[0][i]<ind[1][j])
count++;
*/
j=0;
for(i=0;i<h;i++)
{
if(ind[0][i]<ind[1][j])
{
count=count=count+k-j;
continue;
}
while(ind[0][i]>=ind[1][j])
{
j++;
if (j>=k)
{
break;
}
if(ind[0][i]<ind[1][j])
{
count=count+k-j;
break;
}
}
if (j>=k)
{
break;
}
}
printf("%lld\n",count);
return 0;
}