Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
825 B
6
Indexable
public class Solution {
    
    int isPrime(int A){
        if(A==1){
            return 0;
        }
        
        for(int i=2; i<=Math.sqrt(A); i++){
            if(A%i==0){
                return 0;
            }
        }
        return 1;
    }
    
    public int[] solve(int[] A, int[][] B) {
        int n = A.length;
        int[] prefix = new int[n];
       prefix[0] = isPrime(A[0]);
    
       
        for(int i =1; i<n; i++){
            prefix[i] = prefix[i-1] + isPrime(A[i]);    
        }
          
  
    int m = B.length; 
    int [] ans = new int[m];
    for(int i=0; i<m; i++){
        if(B[i][0]-1>0 ){
            
            ans[i] = prefix[B[i][1]-1] - prefix[B[i][0]-1];
            
        }else{
            ans[i] = prefix[B[i][1]-1];
        }
    }
        return ans;
        
    }
}