CASE 1
#include <stdio.h>
long long arr[10000005];
int main()
{
int n;
int m;
scanf("%d %d", &n, &m);
int index1, index2, summand;
for (int i = 0; i < n; i++)
{
arr[i] = 0;
}
while (m--)
{
scanf("%d %d %d", &index1, &index2, &summand);
arr[index1-1] += summand;
arr[index2] -= summand;
}
long long max = 0, temp = 0;
for (int i = 0; i < n; i++)
{
temp += arr[i];
if (temp >= max)
{
max = temp;
}
}
printf("%lld", max);
}
CASE 2
#include <stdio.h>
int main()
{
int n;
int m;
long long arr[10000005];
scanf("%d %d", &n, &m);
int index1, index2, summand;
for (int i = 0; i < n; i++)
{
arr[i] = 0;
}
while (m--)
{
scanf("%d %d %d", &index1, &index2, &summand);
arr[index1-1] += summand;
arr[index2] -= summand;
}
long long max = 0, temp = 0;
for (int i = 0; i < n; i++)
{
temp += arr[i];
if (temp >= max)
{
max = temp;
}
}
printf("%lld", max);
}
This is the code for array manipulation problem in hackerrank. In the first case the array was declared inside the int main(), But the code execution ended with segmentation fault. In the Second case, the array was declared outside the int main(). Then the code executed with no error msgs through all the test cases.
The storage duration is different:
long long arr[10000005];
outside of any function exists for the entire duration of program execution. This is called static storage duration.long long arr[10000005];
inside a function exists for the duration of execution of the block it is in. This is called automatic storage duration.The amount of space available is different:
The scope of the name is different:
The linkage of the name is different: