Count of subarrays consisting of only prime numbers
Last Updated :
03 May, 2023
Given an array A[] of length N, the task is to find the number of subarrays made up of only prime numbers.
Examples:
Input: arr[] = {2, 3, 4, 5, 7}
Output: 6
Explanation:
All possible subarrays made up of only prime numbers are {{2}, {3}, {2, 3}, {5}, {7}, {5, 7}}
Input: arr[] = {2, 3, 5, 6, 7, 11, 3, 5, 9, 3}
Output: 17
Naive Approach: The simplest approach to solve the problem is to generate all possible subarrays from the given array and check if it made up of only prime numbers or not.
Time Complexity: O(N3 * ?max(array)), where ?M is the time required to check if a number is prime or not and this M can range [min(arr), max(arr)]
Auxiliary Space: O(1)
Two Pointer Approach:
Take two pointers ‘i’ and ‘j’ pointing to the first element of the array (arr[0]). Initialize ans to 0. If the element at index ‘j’ is a prime number, then increase the ans by 1 and increment j by 1. If the element at index ‘j’ is not a prime, increment ‘i’ by 1 and update the value of j to i. Repeat the above steps until ‘i’ reaches the end of array. Return ans.
The implementation of the given method is shown below.
C++
#include <bits/stdc++.h>
using namespace std;
bool is_prime( int n)
{
if (n <= 1)
return 0;
for ( int i = 2; i * i <= n; i++) {
if (n % i == 0)
return 0;
}
return 1;
}
int count_prime_subarrays( int arr[], int n)
{
int ans = 0;
int i = 0;
int j = 0;
while (i < n) {
if (j == n) {
i++;
j = i;
if (i
== n)
break ;
}
if (is_prime(arr[j])) {
ans++;
j++;
}
else {
i++;
j = i;
}
}
return ans;
}
int main()
{
int N = 10;
int ar[] = { 2, 3, 5, 6, 7, 11, 3, 5, 9, 3 };
cout << count_prime_subarrays(ar, N);
}
|
Java
import java.util.*;
public class GFG {
static boolean is_prime( int n)
{
if (n <= 1 )
return false ;
for ( int i = 2 ; i * i <= n; i++) {
if (n % i == 0 )
return false ;
}
return true ;
}
static int count_prime_subarrays( int arr[], int n)
{
int ans = 0 ;
int i = 0 ;
int j = 0 ;
while (i < n) {
if (j == n) {
i++;
j = i;
if (i
== n)
break ;
}
if (is_prime(arr[j])) {
ans++;
j++;
}
else {
i++;
j = i;
}
}
return ans;
}
public static void main(String args[])
{
int N = 10 ;
int ar[] = { 2 , 3 , 5 , 6 , 7 , 11 , 3 , 5 , 9 , 3 };
System.out.print(count_prime_subarrays(ar, N));
}
}
|
Python3
def is_prime(n):
if (n < = 1 ):
return 0 ;
i = 2 ;
while (i * i < = n):
if (n % i = = 0 ):
return 0 ;
i + = 1 ;
return 1 ;
def count_prime_subarrays(arr, n):
ans = 0 ;
i = 0 ;
j = 0 ;
while (i < n) :
if (j = = n) :
i + = 1 ;
j = i;
if (i = = n):
break ;
if (is_prime(arr[j])) :
ans + = 1 ;
j + = 1 ;
else :
i + = 1 ;
j = i;
return ans;
N = 10 ;
ar = [ 2 , 3 , 5 , 6 , 7 , 11 , 3 , 5 , 9 , 3 ];
print (count_prime_subarrays(ar, N));
|
C#
using System;
public class GFG
{
public static bool is_prime( int n)
{
if (n <= 1)
{
return false ;
}
for ( int i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
return false ;
}
}
return true ;
}
public static int count_prime_subarrays( int [] arr, int n)
{
var ans = 0;
var i = 0;
var j = 0;
while (i < n)
{
if (j == n)
{
i++;
j = i;
if (i == n)
{
break ;
}
}
if (GFG.is_prime(arr[j]))
{
ans++;
j++;
}
else
{
i++;
j = i;
}
}
return ans;
}
public static void Main(String[] args)
{
var N = 10;
int [] ar = {2, 3, 5, 6, 7, 11, 3, 5, 9, 3};
Console.Write(GFG.count_prime_subarrays(ar, N));
}
}
|
Javascript
function is_prime(n)
{
if (n <= 1)
return 0;
for (let i = 2; i * i <= n; i++) {
if (n % i == 0)
return 0;
}
return 1;
}
function count_prime_subarrays(arr, n)
{
let ans = 0;
let i = 0;
let j = 0;
while (i < n) {
if (j == n) {
i++;
j = i;
if (i
== n)
break ;
}
if (is_prime(arr[j])) {
ans++;
j++;
}
else {
i++;
j = i;
}
}
return ans;
}
let N = 10;
let ar = [ 2, 3, 5, 6, 7, 11, 3, 5, 9, 3 ];
document.write(count_prime_subarrays(ar, N));
|
Time Complexity: O(N * ?N)
Auxiliary Space: O(1)
Efficient Approach: The following observation needs to be made to optimize the above approach:
Count of subarrays from an array of length M is equal to M * (M + 1) / 2.
Therefore, from a given array, a contiguous subarray of length M consisting only of primes will generate M * (M + 1) / 2 subarrays of length.
Follow the steps below to solve the problem:
- Traverse the array and for every element check if it is a prime or not.
- For every prime number found, keep incrementing count.
- For every non-prime element, update the required answer by adding count * (count + 1) / 2 and reset count to 0.
- Finally, print the required subarray.
Below the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool is_prime( int n)
{
if (n <= 1)
return 0;
for ( int i = 2; i * i <= n; i++) {
if (n % i == 0)
return 0;
}
return 1;
}
int count_prime_subarrays( int ar[], int n)
{
int ans = 0;
int count = 0;
for ( int i = 0; i < n; i++) {
if (is_prime(ar[i]))
count++;
else {
if (count) {
ans += count * (count + 1)
/ 2;
count = 0;
}
}
}
if (count)
ans += count * (count + 1) / 2;
return ans;
}
int main()
{
int N = 10;
int ar[] = { 2, 3, 5, 6, 7,
11, 3, 5, 9, 3 };
cout << count_prime_subarrays(ar, N);
}
|
Java
import java.util.*;
class GFG{
static boolean is_prime( int n)
{
if (n <= 1 )
return false ;
for ( int i = 2 ; i * i <= n; i++)
{
if (n % i == 0 )
return false ;
}
return true ;
}
static int count_prime_subarrays( int ar[], int n)
{
int ans = 0 ;
int count = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (is_prime(ar[i]))
count++;
else
{
if (count != 0 )
{
ans += count * (count + 1 ) / 2 ;
count = 0 ;
}
}
}
if (count != 0 )
ans += count * (count + 1 ) / 2 ;
return ans;
}
public static void main(String[] args)
{
int N = 10 ;
int []ar = { 2 , 3 , 5 , 6 , 7 ,
11 , 3 , 5 , 9 , 3 };
System.out.print(count_prime_subarrays(ar, N));
}
}
|
Python3
def is_prime(n):
if (n < = 1 ):
return 0
i = 2
while (i * i < = n):
if (n % i = = 0 ):
return 0
i + = 1
return 1
def count_prime_subarrays(ar, n):
ans = 0
count = 0
for i in range (n):
if (is_prime(ar[i])):
count + = 1
else :
if (count):
ans + = count * (count + 1 ) / / 2
count = 0
if (count):
ans + = count * (count + 1 ) / / 2
return ans
N = 10
ar = [ 2 , 3 , 5 , 6 , 7 ,
11 , 3 , 5 , 9 , 3 ]
print (count_prime_subarrays(ar, N))
|
C#
using System;
class GFG{
static bool is_prime( int n)
{
if (n <= 1)
return false ;
for ( int i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false ;
}
return true ;
}
static int count_prime_subarrays( int []ar, int n)
{
int ans = 0;
int count = 0;
for ( int i = 0; i < n; i++)
{
if (is_prime(ar[i]))
count++;
else
{
if (count != 0)
{
ans += count * (count + 1) / 2;
count = 0;
}
}
}
if (count != 0)
ans += count * (count + 1) / 2;
return ans;
}
public static void Main(String[] args)
{
int N = 10;
int []ar = { 2, 3, 5, 6, 7,
11, 3, 5, 9, 3 };
Console.Write(count_prime_subarrays(ar, N));
}
}
|
Javascript
<script>
function is_prime(n) {
if (n <= 1) return 0;
for ( var i = 2; i * i <= n; i++) {
if (n % i == 0) return 0;
}
return 1;
}
function count_prime_subarrays(ar, n) {
var ans = 0;
var count = 0;
for ( var i = 0; i < n; i++) {
if (is_prime(ar[i]))
count++;
else {
if (count) {
ans += (count * (count + 1)) / 2;
count = 0;
}
}
}
if (count) ans += (count * (count + 1)) / 2;
return ans;
}
var N = 10;
var ar = [2, 3, 5, 6, 7, 11, 3, 5, 9, 3];
document.write(count_prime_subarrays(ar, N));
</script>
|
Time Complexity: O(N * ?max(arr)), where ?M is the time required to check if a number is prime or not and this M can range [min(arr), max(arr)]
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...