Wrong answer Two test cases failed

I think the Code is accurate but still not passing the test cases

import java.util.*; public class Main { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int n = scn.nextInt(); while (n > 0) { int a = scn.nextInt(); int b = scn.nextInt(); System.out.println(visit(a, b)); n–; } } public static int visit(int a, int b) { int arr[] = new int[b + 1]; for (int i = 0; i <= b; i++) { arr[i] = 1; } for (int i = 2; i * i <= b; i++) { if (arr[i] == 1) { for (int j = i * i; j <= b; j += i) { arr[j] = 0; } } } int count = 0; if (a == 1) { a = a + 1; } for (int i = a; i <= b; i++) { if (arr[i] == 1) { count++; } } return count; } }

@guptadev354,
https://ide.codingblocks.com/s/188664 Here is the corrected code.
Errors:

  1. Use sieve only once. Not again for every test case. And calculate all primes once.
  2. 0 and 1 are not prime.

for (int i = 0; i < arr.length; i++) {
arr[i] = 1;
}
In this line you are assigning them as primes.