📜  仅由质数组成的子数组的计数

📅  最后修改于: 2021-05-17 16:42:08             🧑  作者: Mango

给定长度为N的数组A [] ,任务是查找仅由质数组成的子数组的数量。

例子:

天真的方法:解决问题的最简单方法是从给定数组生成所有可能的子数组,并检查它是否仅由质数组成。
时间复杂度: O(N 3 *√max(array)),其中√M是检查数字是否为质数所需的时间,该M的范围为[min(arr),max(arr)]
辅助空间: O(1)
高效方法:需要进行以下观察以优化上述方法:

因此,从给定的数组中,长度为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)