So I was practicing some algorithm problem and I ran into this problem.. When I type an input of over 800k, it will display a segment fault : 11. So the format for the input is :
number of input
input1
input2
...
and the result should be the amount of number from 0 to 9 that's building the number from 1 till the input, for example if the input is 10 then there is 1 number 0, 2 number 1, 1 number 2, 1 number 3, 1 number 4, and so on till 9. Here is my code that's having the problem and it works just fine if I don't input anything beyond around 200k or something.
#include <stdio.h>
void coba(long long int arr[], long long int y)
{
while(y)
{
arr[y%10]++;
y/=10;
}
}
void hitung(long long int arr[], long long int y,long long int limit)
{
if(y>limit)
{
coba(arr, y);
hitung(arr, y-1,limit);
}
}
int main()
{
int x,m;
scanf("%d",&x);
long long int a[x];
long long int arr[x][10];
for(int i = 0; i<x; i++)
{
scanf("%lld",&a[i]);
for(int j = 0; j<10; j++)
arr[i][j]=0;
}
for(int i = 0; i<x; i++)
{
m=1;
printf("Case #%d: ",i+1);
if(i)
{
for(int j = i-1; j>=0; j--)
{
if(a[i]>a[j])
{
hitung(arr[j], a[i], a[j]);
for(int k = 0; k<9; k++)
{
arr[i][k]=arr[j][k];
printf("%lld ",arr[i][k]);
}
arr[i][9]=arr[j][9];
printf("%lld\n",arr[i][9]);
m=0;
break;
}
}
if(m)
{
hitung(arr[i], a[i], 0);
for(int k = 0; k<9; k++)
{
printf("%lld ",arr[i][k]);
}
printf("%lld\n",arr[i][9]);
}
}
else
{
hitung(arr[i], a[i], 0);
for(int k = 0; k<9; k++)
{
printf("%lld ",arr[i][k]);
}
printf("%lld\n",arr[i][9]);
}
}
}
And there is also a time limit to the question and that's why I'm using memoization here.