Showing TLE error

Please someone help me question is prateek loves candy.

@shivank.agg.sa Main logic behind the question is that you need to find the nth prime number starting from 2. Any one can implement this logic “Not a big Deal” for anyone, but wait a min there’s more to it.

Many of you are getting time limit exceed right ? Even if you are using SOE (Sieve of Eratosthenes) for finding the prime numbers.

Have u see the constraint on number of test cases, It’s quite large because for every test case you are going to find the prime numbers upto that number and at last return the last prime number found, this will cause the TLE for big test cases.

  • So, to overcome this problem, let’s just store your test cases in an array.
  • Then find the maximum number from all of the testcases.
  • then, Use SOE(Sieve of Eratosthenes) to find the prime number upto that number and store that an the array.
  • After storing, simply loop over every test case that was stored previously in an array.
  • Print the value of n from the array storing primes.

That’s how we need not to find the prime number for every test case.
Note : If you haven’t studied SOE yet you will taught later in the course but if you want to learn now refer this link :

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.

i am getting TLE error in this code.

@shivank.agg.sa which question you are talking about? Prateek loves candy ?

yes sir its prateek loves candy

@shivank.agg.sa I already told you to use SOE for this question to avoid TLE approach and link is provided in the Reply Above.


Sir i have used SOE and after using it also i am getting TLE error.
I have pasted the link of my code.

@shivank.agg.sa Refer my code :

import java.util.*;
public class Main {
    public static void main(String args[]) {

        prateekLovesCandy();
    }

    public static void prateekLovesCandy() {

        Scanner scn = new Scanner(System.in);

        int n = scn.nextInt();

        int[] cases = new int[n];
        int max = 0;

        for (int i = 0; i < cases.length; i++) {

            cases[i] = scn.nextInt();
            max = Math.max(max, cases[i]);
        }

        ArrayList primes = getPrimes(max);

        for (int i = 0; i < cases.length; i++) {

            System.out.println(primes.get(cases[i] - 1));
        }
    }

    public static ArrayList getPrimes(int n) {

        boolean[] board = new boolean[1_000_001];

        board[0] = true;
        board[1] = true;

        for (int table = 2; table * table <= 1_000_000; table++) {

            if (!board[table])
                for (int mult = 2; table * mult <= 1_000_000; mult++) {
                    board[table * mult] = true;
                }
        }

        ArrayList ans = new ArrayList<>();

        int cnt = 0;
        for (int i = 2; cnt <= n; i++) {

            if (!board[i]){
                ans.add(i);
                cnt++;
            }
        }

        return ans;
    }

}

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.