给定一个长度为N的数组A[] ,任务是找到仅由素数组成的子数组的数量。
例子:
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
朴素方法:解决问题的最简单方法是从给定数组生成所有可能的子数组,并检查它是否仅由素数组成。
时间复杂度: O(N 3 * √max(array)),其中√M是检查一个数是否为素数所需的时间,这个M 的范围是[min(arr), max(arr)]
辅助空间: O(1)
高效的方法:需要进行以下观察来优化上述方法:
Count of subarrays from an array of length M is equal to M * (M + 1) / 2.
因此,从给定的数组中,仅由素数组成的长度为M的连续子数组将生成M * (M + 1) / 2个长度的子数组。
请按照以下步骤解决问题:
- 遍历数组并检查每个元素是否为素数。
- 对于找到的每个素数,不断增加count 。
- 对于每个非素数元素,通过添加count * (count + 1) / 2并将count重置为0来更新所需的答案。
- 最后,打印所需的子数组。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
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 continous
// 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
// continous 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 continous
// 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
// continous 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 continous
# 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
# continous 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 continous
// 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
// continous 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
17
时间复杂度: O(N * √max(arr)),其中√M是检查一个数是否为素数所需的时间,这个M 的范围是[min(arr), max(arr)]
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live