Number of prefix sum prime in given range query

Given an array of non-negative integers and range query l, r, find number of prefix sum which are prime numbers in that given range.

Prerequisite : Prefix Sum | Primality Test

Examples :



Input : {2, 3, 4, 7, 9, 10}, 
        l = 1, r = 5;
Output : 3
Explanation : prefixSum[0] = arr[l] = 3
         prefixSum[1] = prefixSum[0] + arr[2] = 7, 
         prefixSum[2] = prefixSum[1] + arr[3] = 14, 
         prefixSum[3] = prefixSum[2] + arr[4] = 23, 
         prefixSum[4] = prefixSum[3] + arr[5] = 33,
There are three primes in prefix sum array in given
range. The primes are 3, 7 and 23.

Input : {5, 7, 8, 10, 13}, 
         l = 0, r = 4;
Output : 2
         prefixSum[0] = arr[l] = 5, 
         prefixSum[1] = prefixSum[0] + arr[1] = 12, 
         prefixSum[2] = prefixSum[1] + arr[2] = 20, 
         prefixSum[3] = prefixSum[2] + arr[3] = 30, 
         prefixSum[4] = prefixSum[3] + arr[4] = 43, 
There are two primes in prefix sum array in given
range. The primes are 5 and 43

Approach : Run a loop through l to r, where l and r are the range of indices and fill the prefix sum array which will be of size same as that of array, by adding the previous element of the prefix sum array and present element of the array, then, check whether the prefix sum at each stage is prime or not through Primality Test of checking primes. If the prefix sum is prime than increase the count otherwise not.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to count the number of
// prefix sum which are prime or not 
#include<bits/stdc++.h>
using namespace std;
  
// Primality test to check if prefix 
// sum is prime or not
bool isPrime(int num)
{
    // Corner case
    if (num <= 1) return false;
    if (num <= 3) return true;
  
    // This is checked so that we can skip 
    // middle five numbers in below loop
    if (num%2 == 0 || num%3 == 0) 
        return false;
  
    for (int j = 5; j * j <= num; j = j + 6)
        if (num%j == 0 || num%(j+2) == 0)
        return false;
  
    return true;
}
  
// to calcluate the prefix sum for
// the given range of query
int primesubArraySum(int arr[], int n, int l, 
                     int r, int preSum[])
{
    int count = 0;    
    preSum[0] = arr[l];    
    if (isPrime(preSum[0]))
       count++;
      
      
    for (int i = l + 1, j = 1; 
        i <= r && j < n; i++,j++)
    {
        preSum[j] = preSum[j - 1] + arr[i];
          
        // increase the count if the 
        // prefix sum is prime
        if (isPrime(preSum[j]))
            count++;
    }
      
    return count;
}
  
//driver code
int main()
{
    int arr[] = {5, 7, 8, 10, 13};
    int n = sizeof(arr)/sizeof(arr[0]);
    int preSum[n];
    int l = 0, r = 4;
     
    cout << primesubArraySum(arr, n, l, r, preSum);
      
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to count the number of
// prefix sum which are prime or not 
import java.util.*;
class GFG
{
    // Primality test to check if 
    // prefix sum is prime or not
    public static boolean isPrime(int num)
    {
        // Corner cases
        if (num <= 1
            return false;
        if (num <= 3
            return true;
      
        // This is checked so that we can skip 
        // middle five numbers in below loop
        if (num % 2 == 0 || num % 3 == 0
                return false;
      
        for (int j = 5; j * j <= num; j = j + 6)
            if (num % j == 0 || num % (j + 2) == 0)
                return false;
      
        return true;
    }
      
    // to calcluate the prefix sum for 
    // the given range of query
    public static int primesubArraySum(int arr[], int n, 
                                       int l, int r, 
                                       int preSum[])
    {
        int count = 0;
      
        preSum[0] = arr[l];
      
        if (isPrime(preSum[0]) == true)
            count++;
      
        for (int i = l+1,j = 1; i <= r && j < n; i++,j++)
        {
            preSum[j] = preSum[j-1] + arr[i];
          
            // increase the count if the prefix sum is prime
            if (isPrime(preSum[j]) == true)
                count++;
        }
      
        return count;
    }
  
    // Driver code 
    public static void main (String[] args) 
    {
        int arr[] = {5, 7, 8, 10, 13};
        int n = arr.length;
        int preSum[] = new int[n];
        int l = 0, r = 4;
        System.out.println(primesubArraySum(arr, n, 
                           l, r, preSum));
    }
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to count the number 
# of prefix sum which are prime or not 
import numpy
import math
  
# Primality test to check if prefix 
# sum is prime or not
def isPrime(num):
      
    # Corner case
    if (num <= 1): 
        return False;
    if (num <= 3):
        return True;
  
    # This is checked so that we can skip 
    # middle five numbers in below loop
    if (num % 2 == 0 or num % 3 == 0): 
        return False;
  
    for j in range(5, (int)(math.sqrt(num)) + 1, 6):
        if (num % j == 0 or num % (j + 2) == 0):
            return False;
  
        return True;
  
# to calcluate the prefix sum for
# the given range of query
def primesubArraySum(arr, n, l, r, preSum):
  
    count = 0
    preSum[0] = arr[l]; 
  
    if (isPrime(preSum[0])):
        count = count + 1;
      
      
    for i in range(l + 1, r):
        for j in range(1, n): 
            preSum[j] = preSum[j - 1] + arr[i];
              
            # increase the count if the 
            # prefix sum is prime
            if (isPrime(preSum[j])):
                count = count + 1;
  
    return count;
  
# Driver code
arr = [5, 7, 8, 10, 13];
n = len(arr);
  
#a = numpy.arange(5)
preSum = numpy.arange(n);
l = 0;
r = 4;
print(primesubArraySum(arr, n, l, r, preSum));
      
# This code is contributed
# by Shivi_Aggarwal
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to count the number of
// prefix sum which are prime or not 
using System;
  
class GFG {
      
    // Primality test to check if 
    // prefix sum is prime or not
    public static bool isPrime(int num)
    {
          
        // Corner cases
        if (num <= 1) 
            return false;
        if (num <= 3) 
            return true;
      
        // This is checked so that we can skip 
        // middle five numbers in below loop
        if (num % 2 == 0 || num % 3 == 0) 
                return false;
      
        for (int j = 5; j * j <= num; j = j + 6)
            if (num % j == 0 || num % (j + 2) == 0)
                return false;
      
        return true;
    }
      
    // To calcluate the prefix sum 
    // for the given range of query
    public static int primesubArraySum(int []arr, int n, 
                                       int l, int r, 
                                       int []preSum)
    {
        int count = 0;
      
        preSum[0] = arr[l];
      
        if (isPrime(preSum[0]) == true)
            count++;
      
        for (int i = l+1,j = 1; i <= r && 
                          j < n; i++,j++)
        {
            preSum[j] = preSum[j-1] + arr[i];
          
            // increase the count if the 
            // prefix sum is prime
            if (isPrime(preSum[j]) == true)
                count++;
        }
      
        return count;
    }
  
    // Driver code 
    public static void Main () 
    {
        int []arr = {5, 7, 8, 10, 13};
        int n = arr.Length;
        int []preSum = new int[n];
        int l = 0, r = 4;
        Console.Write(primesubArraySum(arr, n, 
                      l, r, preSum));
    }
}
  
// This code is contributed by Nitin Mittal.
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to count 
// the number of prefix
// sum which are prime
// or not 
  
// Primality test to check if 
// prefix sum is prime or not
function isPrime($num)
{
    // Corner case
    if ($num <= 1) return false;
    if ($num <= 3) return true;
  
    // This is checked so
    // that we can skip 
    // middle five numbers 
    // in below loop
    if ($num % 2 == 0 || 
        $num % 3 == 0) 
        return false;
  
    for ($j = 5; $j * $j <= $num
                 $j = $j + 6)
        if ($num % $j == 0 || 
            $num % ($j + 2) == 0)
        return false;
  
    return true;
}
  
// To calcluate the prefix 
// sum for the given range 
// of query
function primesubArraySum($arr, $n,     
                          $l,$r, $preSum)
{
    $count = 0; 
    $preSum[0] = $arr[$l]; 
    if (isPrime($preSum[0]))
    $count++;
      
    for ($i = $l + 1, $j = 1; 
         $i <= $r && $j < $n
         $i++, $j++)
    {
        $preSum[$j] = $preSum[$j - 1] + 
                              $arr[$i];
          
        // increase the count if  
        // the prefix sum is prime
        if (isPrime($preSum[$j]))
            $count++;
    }
      
    return $count;
}
  
// Driver code
$arr = array(5, 7, 8, 10, 13);
$n = sizeof($arr);
$preSum = array();
$l = 0; $r = 4;
  
echo primesubArraySum($arr, $n, $l
                      $r, $preSum);
      
// This code is contributed by Ajit
?>
chevron_right

Output:
2



Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :