Count of subarrays consisting of only prime numbers
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++
// C++ program to implement the above approach #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' reaches the end of array but 'i' does not // , then change the value of 'j' to 'i' if (j == n) { i++; j = i; if (i == n) // if both 'i' and 'j' reaches the end // of array, we will break the loop break ; } if (is_prime(arr[j])) { ans++; // we will increment the count if 'j' is // prime j++; // we will increment 'j' by 1 } // if arr[j] is not prime else { i++; // we will increment i by 1 j = i; // assign the value of i to j } } return ans; } // Driver Code int main() { int N = 10; int ar[] = { 2, 3, 5, 6, 7, 11, 3, 5, 9, 3 }; cout << count_prime_subarrays(ar, N); } |
Java
// Java program to of the above approach 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' reaches the end of array but 'i' does not // , then change the value of 'j' to 'i' if (j == n) { i++; j = i; if (i == n) // if both 'i' and 'j' reaches the end // of array, we will break the loop break ; } if (is_prime(arr[j])) { ans++; // we will increment the count if 'j' is // prime j++; // we will increment 'j' by 1 } // if arr[j] is not prime else { i++; // we will increment i by 1 j = i; // assign the value of i to j } } return ans; } // Driver Code 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)); } } // This code is contributed by code_hunt. |
Python3
# Python program to implement the above approach 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' reaches the end of array but 'i' does not # , then change the value of 'j' to 'i' if (j = = n) : i + = 1 ; j = i; if (i = = n): # if both 'i' and 'j' reaches the end # of array, we will break the loop break ; if (is_prime(arr[j])) : ans + = 1 ; # we will increment the count if 'j' is # prime j + = 1 ; # we will increment 'j' by 1 # if arr[j] is not prime else : i + = 1 ; # we will increment i by 1 j = i; # assign the value of i to j return ans; # Driver Code N = 10 ; ar = [ 2 , 3 , 5 , 6 , 7 , 11 , 3 , 5 , 9 , 3 ]; print (count_prime_subarrays(ar, N)); # This code is contributed by poojaagarwal2. |
C#
// Include namespace system 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' reaches the end of array but 'i' does not // , then change the value of 'j' to 'i' if (j == n) { i++; j = i; if (i == n) { // if both 'i' and 'j' reaches the end // of array, we will break the loop break ; } } if (GFG.is_prime(arr[j])) { ans++; // we will increment the count if 'j' is // prime j++; } else { i++; // we will increment i by 1 j = i; } } return ans; } // Driver Code 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)); } } // This code is contributed by sourabhdalal0001. |
Javascript
// Javascript program to implement the above approach 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' reaches the end of array but 'i' does not // , then change the value of 'j' to 'i' if (j == n) { i++; j = i; if (i == n) // if both 'i' and 'j' reaches the end // of array, we will break the loop break ; } if (is_prime(arr[j])) { ans++; // we will increment the count if 'j' is // prime j++; // we will increment 'j' by 1 } // if arr[j] is not prime else { i++; // we will increment i by 1 j = i; // assign the value of i to j } } return ans; } // Driver Code let N = 10; let ar = [ 2, 3, 5, 6, 7, 11, 3, 5, 9, 3 ]; document.write(count_prime_subarrays(ar, N)); |
17
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++
// C++ Program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to check if a number // is prime or not. bool is_prime( int n) { if (n <= 1) return 0; for ( int i = 2; i * i <= n; i++) { // If n has any factor other than 1, // then n is non-prime. if (n % i == 0) return 0; } return 1; } // Function to return the count of // subarrays made up of prime numbers only int count_prime_subarrays( int ar[], int n) { // Stores the answer int ans = 0; // Stores the count of continuous // prime numbers in an array int count = 0; for ( int i = 0; i < n; i++) { // If the current array // element is prime if (is_prime(ar[i])) // Increase the count count++; else { if (count) { // Update count of subarrays ans += count * (count + 1) / 2; count = 0; } } } // If the array ended with a // continuous prime sequence if (count) ans += count * (count + 1) / 2; return ans; } // Driver Code int main() { int N = 10; int ar[] = { 2, 3, 5, 6, 7, 11, 3, 5, 9, 3 }; cout << count_prime_subarrays(ar, N); } |
Java
// Java Program to implement // the above approach import java.util.*; class GFG{ // Function to check if a number // is prime or not. static boolean is_prime( int n) { if (n <= 1 ) return false ; for ( int i = 2 ; i * i <= n; i++) { // If n has any factor other than 1, // then n is non-prime. if (n % i == 0 ) return false ; } return true ; } // Function to return the count of // subarrays made up of prime numbers only static int count_prime_subarrays( int ar[], int n) { // Stores the answer int ans = 0 ; // Stores the count of continuous // prime numbers in an array int count = 0 ; for ( int i = 0 ; i < n; i++) { // If the current array // element is prime if (is_prime(ar[i])) // Increase the count count++; else { if (count != 0 ) { // Update count of subarrays ans += count * (count + 1 ) / 2 ; count = 0 ; } } } // If the array ended with a // continuous prime sequence if (count != 0 ) ans += count * (count + 1 ) / 2 ; return ans; } // Driver Code 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)); } } // This code is contributed by PrinciRaj1992 |
Python3
# Python3 program to implement # the above approach # Function to check if a number # is prime or not. def is_prime(n): if (n < = 1 ): return 0 i = 2 while (i * i < = n): # If n has any factor other than 1, # then n is non-prime. if (n % i = = 0 ): return 0 i + = 1 return 1 # Function to return the count of # subarrays made up of prime numbers only def count_prime_subarrays(ar, n): # Stores the answer ans = 0 # Stores the count of continuous # prime numbers in an array count = 0 for i in range (n): # If the current array # element is prime if (is_prime(ar[i])): # Increase the count count + = 1 else : if (count): # Update count of subarrays ans + = count * (count + 1 ) / / 2 count = 0 # If the array ended with a # continuous prime sequence if (count): ans + = count * (count + 1 ) / / 2 return ans # Driver Code N = 10 ar = [ 2 , 3 , 5 , 6 , 7 , 11 , 3 , 5 , 9 , 3 ] # Function call print (count_prime_subarrays(ar, N)) # This code is contributed by Shivam Singh |
C#
// C# Program to implement // the above approach using System; class GFG{ // Function to check if a number // is prime or not. static bool is_prime( int n) { if (n <= 1) return false ; for ( int i = 2; i * i <= n; i++) { // If n has any factor other than 1, // then n is non-prime. if (n % i == 0) return false ; } return true ; } // Function to return the count of // subarrays made up of prime numbers only static int count_prime_subarrays( int []ar, int n) { // Stores the answer int ans = 0; // Stores the count of continuous // prime numbers in an array int count = 0; for ( int i = 0; i < n; i++) { // If the current array // element is prime if (is_prime(ar[i])) // Increase the count count++; else { if (count != 0) { // Update count of subarrays ans += count * (count + 1) / 2; count = 0; } } } // If the array ended with a // continuous prime sequence if (count != 0) ans += count * (count + 1) / 2; return ans; } // Driver Code 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)); } } // This code is contributed by Rajput-Ji |
Javascript
<script> // JavaScript Program to implement // the above approach // Function to check if a number // is prime or not. function is_prime(n) { if (n <= 1) return 0; for ( var i = 2; i * i <= n; i++) { // If n has any factor other than 1, // then n is non-prime. if (n % i == 0) return 0; } return 1; } // Function to return the count of // subarrays made up of prime numbers only function count_prime_subarrays(ar, n) { // Stores the answer var ans = 0; // Stores the count of continuous // prime numbers in an array var count = 0; for ( var i = 0; i < n; i++) { // If the current array // element is prime if (is_prime(ar[i])) // Increase the count count++; else { if (count) { // Update count of subarrays ans += (count * (count + 1)) / 2; count = 0; } } } // If the array ended with a // continuous prime sequence if (count) ans += (count * (count + 1)) / 2; return ans; } // Driver Code var N = 10; var ar = [2, 3, 5, 6, 7, 11, 3, 5, 9, 3]; document.write(count_prime_subarrays(ar, N)); </script> |
17
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)
Please Login to comment...