Easy Math | CodeChef

Why I am getting a sigsegv error in this code during submission.

#include <bits/stdc++.h>
using namespace std;
#define max 500

int sum_digit(int n)
{
int digit_sum = 0;
while (n)
{
digit_sum += n % 10;
n /= 10;
}
return digit_sum;
}

int main() {
int t ;
cin >> t;
while (t–)
{
int n;
cin>>n;
int a[n],prod[max],k=-1;
for(int i=0;i<n;i++)
cin>>(a[i]);

    for(int i=0;i<n-1;i++)
    {
        for(int j=i+1;j<n;j++)
            prod[++k]=a[i]*a[j];
    }
    for(int i=0;i<=k;i++)
        prod[i]=sum_digit(prod[i]);
        
    sort(prod,prod+(k+1));
    cout<<prod[k]<<endl;
}
return 0;

}

for(int i=0;i<n-1;i++)
    {
        for(int j=i+1;j<n;j++)
            prod[++k]=a[i]*a[j];
    }

Observe this, this nested loop is O(n^2). And constraints say max value of n is 100, so that’s why change:
#define max 500
to
something like,
#define max 10009

2 Likes

@ks3rr thanks for the help.It worked.