Open In App

Sub array sum is prime or not

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array and limits (lower and upper limits), check the sum of the subarray in the given limit is prime or not 

Examples : 

Input  :  a[] = {1, 2, 3, 5, 5, 4, 7, 8, 9};
          lower = 3, upper = 6
Output :  Yes
Explanation:- subarray is {3, 5, 5, 4} and 
sum of subarray 3+5+5+4 = 17 which is prime, so
the output is yes  

Input  :  a[] = {1, 6, 4, 5, 5, 4, 7, 8, 9};
          lower = 2, upper = 5
Output :  No
Explanation:- subarray is {6, 4, 5, 5} and sum
of subarray 6+4+5+5 = 20 which is Not prime so the
output is No  
  1. First calculate the sum of sub-array using upper limit and lower limit 
  2. Then check the sum is prime or not. 
  3. If it is prime then return true otherwise return false. 

Let’s understand this approach using code below. 

C++




// A C++ program to check the given
// subarray is prime or not
#include <iostream>
using namespace std;
 
// function to check the array
bool isPrime(int a[], int lower,
             int upper)
{
    int n = 0;
 
    // Calculate the sum of
    // the subarray
    for (int i = lower - 1;
         i <= upper - 1;
         i++)
        n += a[i];
 
    // check the sum of the
    // subarray is prime or
    // not (Corner case)
    if (n <= 1)
        return false;
 
    // Check from 2 to n - 1
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;
 
    return true;
}
 
// Driver Code
int main()
{
    // taking input
    int a[] = { 1, 2, 3, 5, 5,
                4, 7, 8, 9 };
    int lower = 3, upper = 6;
    if (isPrime(a, lower, upper))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}


Java




// A java program to check the given
// subarray is prime or not
import java.io.*;
 
public class GFG {
 
    // function to check the array
    static boolean isPrime(int a[],
                           int lower,
                           int upper)
    {
        int n = 0;
 
        // Calculate the sum of
        // the subarray
        for (int i = lower - 1;
             i <= upper - 1; i++)
            n += a[i];
 
        // check the sum of the
        // subarray is prime or
        // not (Corner case)
        if (n <= 1)
            return false;
 
        // Check from 2 to n-1
        for (int i = 2; i < n; i++)
            if (n % i == 0)
                return false;
 
        return true;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // taking input
        int a[] = { 1, 2, 3, 5, 5, 4, 7, 8, 9 };
        int lower = 3, upper = 6;
 
        if (isPrime(a, lower, upper))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
 
// This code is contributed by Sam007.


Python3




# A Python3 program to check the given
# subarray is prime or not
 
# function to check the array
def isPrime(a, lower, upper) :
    n = 0
 
    # Calculate the sum of
    # the subarray
    for i in range(lower - 1, upper) :
        n = n + a[i]
 
    # check the sum of the
    # subarray is prime or
    # not (Corner case)
    if (n <= 1) :
        return False
 
    # Check from 2 to n - 1
    for i in range(2, n) :
        if (n % i == 0) :
            return False
     
    return True
 
# Driver Code
 
# taking input
a = [1, 2, 3, 5, 5, 4, 7, 8, 9]
lower = 3
upper = 6
if (isPrime(a, lower, upper)) :
    print ("Yes")
else :
    print ("No")
 
# This code is contributed by
# Manish Shaw (manishshaw1)


C#




// A C# program to check the given
// subarray is prime or not
using System;
 
class GFG {
    // function to check the array
    static bool isPrime(int[] a,
                        int lower,
                        int upper)
    {
        int n = 0;
 
        // Calculate the sum of
        // the subarray
        for (int i = lower - 1;
             i <= upper - 1;
             i++)
            n += a[i];
 
        // check the sum of the
        // subarray is prime or
        // not (Corner case)
        if (n <= 1)
            return false;
 
        // Check from 2 to n-1
        for (int i = 2; i < n; i++)
            if (n % i == 0)
                return false;
 
        return true;
    }
 
    // Driver Code
    public static void Main()
    {
        // taking input
        int[] a = { 1, 2, 3, 5, 5,
                    4, 7, 8, 9 };
        int lower = 3, upper = 6;
        if (isPrime(a, lower, upper))
            Console.Write("Yes");
        else
            Console.Write("No");
    }
}
 
// This code is contributed by Sam007


PHP




<?php
// A PHP program to check the given
// subarray is prime or not
 
// function to check the array
function isPrime($a, $lower, $upper)
{
    $n = 0;
 
    // Calculate the sum of
    // the subarray
    for ($i = $lower - 1;
            $i <= $upper - 1; $i++)
        $n += $a[$i];
 
    // check the sum of the
    // subarray is prime or
    // not (Corner case)
    if ($n <= 1)
        return false;
 
    // Check from 2 to n - 1
    for ($i = 2; $i < $n; $i++)
        if ($n % $i == 0)
            return false;
     
    return true;
}
 
    // Driver Code
 
    // taking input
    $a = array(1, 2, 3, 5, 5,
                  4, 7, 8, 9);
    $lower = 3; $upper = 6;
    if (isPrime($a, $lower, $upper))
        echo "Yes", " \n";
    else
        echo "No", " \n";
 
// This code is contributed by ajit
?>


Javascript




<script>
 
// A Javascript program to check the given
// subarray is prime or not
 
// Function to check the array
function isPrime(a, lower, upper)
{
    let n = 0;
 
    // Calculate the sum of
    // the subarray
    for(let i = lower - 1;
            i <= upper - 1; i++)
        n += a[i];
 
    // Check the sum of the
    // subarray is prime or
    // not (Corner case)
    if (n <= 1)
        return false;
 
    // Check from 2 to n-1
    for(let i = 2; i < n; i++)
        if (n % i == 0)
            return false;
 
    return true;
}
 
// Driver code
 
// Taking input
let a = [ 1, 2, 3, 5, 5, 4, 7, 8, 9 ];
let lower = 3, upper = 6;
 
if (isPrime(a, lower, upper))
    document.write("Yes");
else
    document.write("No");
     
// This code is contributed by rameshtravel07
 
</script>


Output

Yes

Time Complexity: O(n)
Auxiliary Space: O(1)



Last Updated : 16 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads