Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
44
Task/Equilibrium-index/C/equilibrium-index.c
Normal file
44
Task/Equilibrium-index/C/equilibrium-index.c
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int list[] = {-7, 1, 5, 2, -4, 3, 0};
|
||||
|
||||
int eq_idx(int *a, int len, int **ret)
|
||||
{
|
||||
int i, sum, s, cnt;
|
||||
/* alloc long enough: if we can afford the original list,
|
||||
* we should be able to afford to this. Beats a potential
|
||||
* million realloc() calls. Even if memory is a real concern,
|
||||
* there's no garantee the result is shorter than the input anyway */
|
||||
cnt = s = sum = 0;
|
||||
*ret = malloc(sizeof(int) * len);
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
sum += a[i];
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
if (s * 2 + a[i] == sum) {
|
||||
(*ret)[cnt] = i;
|
||||
cnt++;
|
||||
}
|
||||
s += a[i];
|
||||
}
|
||||
|
||||
/* uncouraged way to use realloc since it can leak memory, for example */
|
||||
*ret = realloc(*ret, cnt * sizeof(int));
|
||||
|
||||
return cnt;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int i, cnt, *idx;
|
||||
cnt = eq_idx(list, sizeof(list) / sizeof(int), &idx);
|
||||
|
||||
printf("Found:");
|
||||
for (i = 0; i < cnt; i++)
|
||||
printf(" %d", idx[i]);
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue