Gives error due to large size of array

Below is given code. I have tried vectors as well as array using new to use heap memory for large size.
However, I still get a runtime error. Any quick help will be appreciated. This code runs fine obviously for upper limit<=10000. However, in the question the upper limit is 10^6, I have tried the check prime logic as well, it gives TLE on 1st test case.

#include<bits/stdc++.h>
const int sizearr = 10000;
using namespace std;
int main(void){
int n;
cin>>n;
bool arr[sizearr];
for(int i=0;i<sizearr;i++){
arr[i] = true;
}
arr[0] = false;
arr[1] = false;
for(int i=2;i<sizearr;i++){
if(arr[i] == false){
continue;
}
for(int j=i*i;j<sizearr;j+=i){
arr[j]=false;
}
}
int arr1[sizearr];
arr1[0] = 0;
for(int i=1;i<sizearr;i++){
if(arr[i]==true){
arr1[i] = arr1[i-1]+1;
continue;
}
arr1[i] = arr1[i-1];
}
//delete []arr;
/for(int i=0;i<sizearr;i++){
cout<<arr1[i]<<" ";
}
cout<<endl;
/
while(n–){
int left,right;
cin>>left>>right;
cout<<arr1[right] - arr1[left-1]<<endl;
}
//delete []arr1;
}

Hello @Arpan_Goswami,

Always Share your code using Online Coding Blocks IDE.
The way you have shared it may introduce much syntax error to it.
STEPS:

  1. Paste it at https://ide.codingblocks.com/
  2. Save your code there
  3. Share the URL generated

Now, coming back to our code:
As the value of a and b can be anything in the range [1,1000000] and N can be 1000.
Your code will face TLE.

Reason:
The time complexity of your code is O(Nblog(b)) which is very high.

Solution:
Rather use prime sieves method to solve this problem.

Hope, this would help.
Give a like if you are satisfied.

https://ide.codingblocks.com/s/165901 The code as per your logic. If you decrease the size of array it seems to work fine to say 10^4 instead of 10^6.The TLE thing which I asked for was if I implemented the checkprime logic i.e check a number is prime or not if it has more than 2 factors for all numbers in the range of [a,b]. I am pretty sure the code which I had sent above too wouldn’t have given TLE as I was getting runtime error instead of TLE in the code that I had shared before. I suspect the time complexity now should be O(Nblog(log(b))).Any help will be appreciated. Thanks and have a nice day.

**Time complexity will be O(N+bloglogb)

https://ide.codingblocks.com/s/165923 The C++ code gives runtime error due to size of the array. This was my doubt all along

Hey @Arpan_Goswami,

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.