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
- First calculate the sum of sub-array using upper limit and lower limit
- Then check the sum is prime or not.
- If it is prime then return true otherwise return false.
Let’s understand this approach using code below.
C++
#include <iostream>
using namespace std;
bool isPrime( int a[], int lower,
int upper)
{
int n = 0;
for ( int i = lower - 1;
i <= upper - 1;
i++)
n += a[i];
if (n <= 1)
return false ;
for ( int i = 2; i < n; i++)
if (n % i == 0)
return false ;
return true ;
}
int main()
{
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
import java.io.*;
public class GFG {
static boolean isPrime( int a[],
int lower,
int upper)
{
int n = 0 ;
for ( int i = lower - 1 ;
i <= upper - 1 ; i++)
n += a[i];
if (n <= 1 )
return false ;
for ( int i = 2 ; i < n; i++)
if (n % i == 0 )
return false ;
return true ;
}
public static void main(String[] args)
{
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" );
}
}
|
Python3
def isPrime(a, lower, upper) :
n = 0
for i in range (lower - 1 , upper) :
n = n + a[i]
if (n < = 1 ) :
return False
for i in range ( 2 , n) :
if (n % i = = 0 ) :
return False
return True
a = [ 1 , 2 , 3 , 5 , 5 , 4 , 7 , 8 , 9 ]
lower = 3
upper = 6
if (isPrime(a, lower, upper)) :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static bool isPrime( int [] a,
int lower,
int upper)
{
int n = 0;
for ( int i = lower - 1;
i <= upper - 1;
i++)
n += a[i];
if (n <= 1)
return false ;
for ( int i = 2; i < n; i++)
if (n % i == 0)
return false ;
return true ;
}
public static void Main()
{
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" );
}
}
|
PHP
<?php
function isPrime( $a , $lower , $upper )
{
$n = 0;
for ( $i = $lower - 1;
$i <= $upper - 1; $i ++)
$n += $a [ $i ];
if ( $n <= 1)
return false;
for ( $i = 2; $i < $n ; $i ++)
if ( $n % $i == 0)
return false;
return true;
}
$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" ;
?>
|
Javascript
<script>
function isPrime(a, lower, upper)
{
let n = 0;
for (let i = lower - 1;
i <= upper - 1; i++)
n += a[i];
if (n <= 1)
return false ;
for (let i = 2; i < n; i++)
if (n % i == 0)
return false ;
return true ;
}
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" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!