Code not working!

I WROTE THE CODE INCLUDING ALL THE POSSIBLE TEST CASES BUT AS SOON AS I SUBMIT THE CODE , IT SHOWS “WRONG-OUTPUT”

import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); if(b>a) { if(a<1) { System.out.println(“Invalid Input”); } else if(a>=1) { int count = 0 , counter = 0; if(a==1) { a=3 ; count = 1;} if(a==2) { count =1; a++;} else { count = 0;} for(int i = a ; i<= b ;i++) { counter = 0; for(int j =2 ; jj<=i ; j++) { if(i%j==0) {counter = 1; break;} } if(counter == 0) { count ++;} } System.out.println(count); } } else if(a==b&&a>=1) { if(a==2) {System.out.println(“1”);} else {int counter = 0; for(int i = 2 ; ii <= a ;i++) { if(a%i==0) {counter = 1; break;} } if(counter == 0) { System.out.println(“1”);} } } else{ System.out.println(“Invalid Input”); } } }

Hi Manan,
for (int i = a; i <= b; i++) {
counter = 0;
for (int j = 2; j <= i; j++) {
if (i % j == 0) {
counter = 1;
break;
}
}
if (counter == 0) {
count++;
}

In the above mentioned part of your code, if you go till j<=i then i%j will definitely be equal to 0 hence count will not be updated. Also, recheck the input format in your approach, you need to input number of test cases first. Also, after resolving these errors, you can still get a TLE error. It will be because of the code I mentioned above, try to figure it out else ping me.

ACTUALLY THE LOGIC WAS THAT j*j <=i and not j <=i

Oh. But it’s not clear in the code you have sent. Please send it again using an IDE. Also, change your input format. You need to input number of test cases first

import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if(n>0) // INPUT { int [][] arr = new int [n][2]; for(int i =0;i<n ;i++) { arr[i][0] = sc.nextInt(); arr[i][1] = sc.nextInt(); } for(int k = 0 ; k < n ; k++) { if(arr[k][0]>=1&&arr[k][1]>1&&arr[k][1]>arr[k][0]) // CONDITION CHECK { int count = 0 , counter = 0; if(arr[k][0]==1||arr[k][0]==2) { arr[k][0]=3 ; count = 1;} else { count = 0;} for(int i = arr[k][0] ; i<= arr[k][1] ;i++) { counter = 0; for(int j =2 ; j*j<=i ; j++) { if(i%j==0) {counter = 1; break;} } if(counter == 0) { count ++;} } System.out.println(count); } else { System.out.println(“Invalid Input”); }} } else{ System.out.println(“Invalid Input”); } } }

I AM FACING A WRONG ANSWER ERROR[[[ in test case 1 ]]] AND A TIME LIMIT ERROR [[[ in test case 3]]]

Hi Manan,
first of all there’s no need to write invalid input until and unless its given in the question. If you have been provided the constraints this means that the input provided by the tester will take care of itself.

Second, to deal with TLE you need to use Sieve technique where you create a boolean array of b+1 size. And start from the first prime number i.e. 2. Take its square(4) and increment it by the number you took (i.e. 4,6,8,10 and so on). Mark all these as false since they cannot be primes. Now take the next unmarked number, 3. And mark 9,12,15 and so as false. Similarly do it for 4. 16,20,24 and so on as false. When you finish the loop, count all the positions marked as true. This will be our final answer.

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.