The major change which i done is
-
make array global
Reason
actually when you have declared that array in function it was created in stack memory and when you were printing something compiler got some time and releases some memory which helped in processing and printing the output.
Its resolved by creating the array globally because the global arrays are created in heap memory.
-
instead of int use ll(long long int) this is required as n is large
-
using vector
this is not to optimise
actually intially i did not understand your approach and also it is giving wrong answer that’s why i use vector
but now when i see again i got your mistake
you can also use your appoach also
Modified Code
#include<iostream>
using namespace std;
#define ll long long int
bool arr[100000004];
int main() {
ll num, val;
cin >> val;
if(val==1){cout<<2<<endl;return 0;}
num = 100000001;
arr[2] = 1;
val--;
//mark all odd numbers as prime
for (ll i = 3; i < num; i = i + 2)
{ arr[i] = 1; }
//as 1 prime of 2 already accounted
for (ll i = 3; i < num; i = i + 2)
{
if (!arr[i]);
else{val--;
if (val == 0) {cout << i << endl; return 0; }
for (ll j = i * i; j < num; j = j + i)
{arr[j] = 0;}}
}
return 0;
}
and this time it is same code which passing testcase 
i hope this help
if you have more doubts regarding this feel free to ask
if your doubt is resolved mark it as resolved from your doubt section inside your course