Help Me Overcome this SIGSEGV Error !

Click here for Problem Statement

SIGSEGV Error is Irritating me a lot . Please help me overcome this problem !
What Should I do to Overcome this SIGSEGV Error. I had allocated memory correctly but still it shows this error :frowning:
P.S.: My First D.P.
THANKS A LOT IN ADVANCE :slight_smile:

#include< iostream >

using namespace std;

long long int mem[1000001],temp;

long long int f(long long int x)

{

if(mem[x]) 

{ 

    return mem[x]; 

} 

else if(x<6) 

{ 

        return x; 

} 

else 

{ 

        temp=f(x/2)+f(x/3)+f(x/4); 

} 

mem[x]=temp; 

return temp; 

}

int main()

{

ios::sync_with_stdio(false); 

cin.tie(0); 

long long int n,i; 

for ( i = 0 ; i < 1000001 ; i++ ) 

{ 

    mem [ i ] = 0; 

} 

while ( cin >> n ) 

{ 

    cout << max(n,f(n)); 

    cout << endl; 

}

return 0;

}

you are accessing the array mem out of bound , mem array is of 10^6 size , whereas n can be upto 10^9 ,

@vsukeeshbabu, value of n can be upto 10^9, so you cannot create an array that big. Also, do not need to memoize for ALL values less than n, so in order to only store those values you require, and quickly and conveniently access them in logarithmic time, you can use a map. A map behaves like a function. You give an input to it, and it gives a required output. It is stored in the form of a heap, so it can be updated and queried in logn time. Click here and here for map reference. You can use it just like an array.

1 Like

thanks Vaibhav @gvaibhav21 :stuck_out_tongue: !!

you’re welcome :slight_smile: