Wrong answer for first two test cases

Please go through the code once. When I keep the value of M (i.e total primesieve array size) around 10^7, the code is working fine(with only two out of six test cases wrong). When I increase the value of M to account for the larger prime numbers, I am getting compilation Time exceeded error(which is understandable). But, I simply used the standard
prime sieve function taught in the videos. Also I considered a SET which I integrated in the prime sieve function itself (It is simultaneously building the SET whilst building the primesieve array) and thus not adding any extra complexity as such. So Is there any other efficient algorithm for prime sieve other the one shown in video? Is there any other way to account for larger prime values without getting TLE? Kindly look into it if I am making some other technical error.

Also time limit is exceeding 10^7 onwards. Till 10^6 its working fine (except the first two test cases)

The said numbers(10^7/10^6) are values of M

okay , so I removed the SET implem

Okay, so I removed the SET implementation and simply traversed the ā€œpā€ array once again and pushed the prime numbers into the prime vector. And the code is working fine with no test cases wrong and also takes in large values of M(10^8). But, any further insights ,as to why the SET implemented version was not working?

@a_krisna22 you cannot access the required element directly in set while using array you can just print prime[k-1] as your answer but to print it using set first you will need to find the pointer to that element and hence it will take time.