-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzad1_ms.c
43 lines (37 loc) · 983 Bytes
/
zad1_ms.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
#include <stdio.h>
#include <stdlib.h>
int maxx(int a, int b)
{
if (a > b)
return a;
return b;
}
// let: a starts at index 0
int *zsumuj(int n, int a[])
{
int *suffixSum = (int *)calloc((size_t)n, sizeof(int));
suffixSum[n - 1] = a[n - 1];
for (int i = n - 1 - 1; i >= 0; i--)
{
suffixSum[i] = suffixSum[i + 1] + a[i];
}
int *b = (int *)malloc((size_t)n * sizeof(int));
for (int i = 0; i < n; i++)
{
if (a[i] >= n)
b[i] = 0;
else{
// we start at 0(in task at 1), so we must decrease value by 1
int movedLeft = a[i]-1;
b[i] = suffixSum[maxx(movedLeft,0)];
}
}
return b;
}
int main(){
int n; if(!scanf("%d", &n)) printf("wrong input");
int* a = (int*)malloc((size_t)n*sizeof(int));
for(int i = 0; i<n; i++) if(!scanf("%d",&a[i])) printf("wrong input");
int* b = zsumuj(n,a);
for(int i = 0; i<n; i++) printf("%d\n", b[i]);
}