What's wrong (Sahil the Anime Lover), for some test cases it's showing RE (SIGSEGV)

#include
#include
#define ll long long
using namespace std;

ll sumOPrime(ll n, vector &isPrime){
ll sum=0;
for(ll i=0; i<=n; i++ )
if(isPrime[i])
sum+=i;
return sum;
}
int main() {
int testCases, n;
vector isPrime(100000,true);
isPrime[0]=false;
isPrime[1]=false;
for(ll i=0; i<=100000; i++)
if(isPrime[i])
for(ll j=i*2; j<=100000; j+=i)
isPrime[j]=false;
cin>>testCases;
while(testCases!=0){
scanf("%d", &n);
cout<<sumOPrime(n,isPrime)<<endl;
testCases–;
}
return 0;
}Preformatted text

I guess I found the error. You are accessing invalid memory location in for loop. Instead of i<=100000, it should be i<100000. Same with the inner loop too. And btw, the code’s time complexity can be optimised to a great extent.

1)but i’s working fine for the test case
2
1
2
2) can u tell me where I can optimize?

@pruthvi27, the max limit of N is 10^7. So you will need to take array size 10^7. Also try to pre-compute the sum of primes initially in another array, so that you can answer each testcase in O(1) instead of O(n).