在给定的数组中查找具有素数和的最长子数组
给定一个数组arr [],任务是找到和为素数的最长子数组。
例子:
Input: arr[ ] = {1, 4, 2, 1}
Output: 3
Explaination: 4+2+1=7 and 7 is a prime number thus the subarray we get is {4, 2, 1} .
Input: arr[ ] = {5, 2, 11, 4, 7, 19}
Output: 5
Explaination: 5+2+11+4+7=29 and 29 is a prime number thus the subarray we get is {5, 2, 11, 4, 7, 19} .
方法:这个问题可以通过生成所有子数组并检查每个子数组的和是否为素数来解决。可以使用 Eratosthenes 的检查素筛。任何子数组的最大和都可以等于原始数组的所有元素之和。按照步骤解决给定的问题。
- 创建一个total_sum变量,它存储arr[]中所有元素的总和。
- 做 Eratosthenes 筛直到total_sum因为 任何子数组的总和都不能大于该值。
- 创建一个max_sum变量来跟踪我们在生成所有子数组时获得的最大子数组。
- 使用两个循环查找 arr[]的所有子数组的总和,对于每个子数组,检查总和是否为素数。
- 在总和为素数的子数组中取最大值。
- 打印 max_sum 作为所需答案。
下面是上述方法的实现。
C++
// C++ program for above approach
#include
using namespace std;
// Utility function to find whether number is
// prime or not
void SieveOfEratosthenes(
vector& prime, int total_sum)
{
// Create a boolean array "prime[0..n]" and
// initialize all entries it as true.
// A value in prime[i] will finally be false
// if i is Not a prime, else true.
for (int i = 0; i <= total_sum; i++) {
prime[i] = true;
}
for (int p = 2; p * p <= total_sum; p++) {
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true) {
// Update all multiples of p
// greater than or equal to the square
// of it numbers which are multiple of p
// and are less than p^2 are already
// been marked.
for (int i = p * p; i <= total_sum; i += p)
prime[i] = false;
}
}
}
int maxLenSubarrayWithSumPrime(
vector& arr, int n)
{
// to store total_sum of original array
int total_sum = 0;
// calculate total_sum
for (int i = 0; i < n; i++) {
total_sum += arr[i];
}
// to store whether the number is
// prime or not
vector prime(total_sum + 1);
// calling sieve to get prime values
SieveOfEratosthenes(prime, total_sum);
// to keep track of current and
// maximum sum till now
int max_sum = 0, cur_sum = 0;
for (int i = 0; i < n; i++) {
cur_sum = 0;
for (int j = i; j < n; j++) {
cur_sum += arr[j];
// is current sum is prime
if (prime[cur_sum]) {
max_sum = max(max_sum, j - i + 1);
}
}
}
// return maximum sum founded.
return max_sum;
}
// Driver Code
int main()
{
int n = 6;
vector arr = { 5, 2, 11, 4, 7, 19 };
cout << maxLenSubarrayWithSumPrime(arr, n);
}
Java
// Java program for above approach
import java.util.*;
class GFG{
// Utility function to find whether number is
// prime or not
static void SieveOfEratosthenes(
boolean []prime, int total_sum)
{
// Create a boolean array "prime[0..n]" and
// initialize all entries it as true.
// A value in prime[i] will finally be false
// if i is Not a prime, else true.
for (int i = 0; i <= total_sum; i++) {
prime[i] = true;
}
for (int p = 2; p * p <= total_sum; p++) {
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true) {
// Update all multiples of p
// greater than or equal to the square
// of it numbers which are multiple of p
// and are less than p^2 are already
// been marked.
for (int i = p * p; i <= total_sum; i += p)
prime[i] = false;
}
}
}
static int maxLenSubarrayWithSumPrime(
int[] arr, int n)
{
// to store total_sum of original array
int total_sum = 0;
// calculate total_sum
for (int i = 0; i < n; i++) {
total_sum += arr[i];
}
// to store whether the number is
// prime or not
boolean []prime = new boolean[total_sum + 1];
// calling sieve to get prime values
SieveOfEratosthenes(prime, total_sum);
// to keep track of current and
// maximum sum till now
int max_sum = 0, cur_sum = 0;
for (int i = 0; i < n; i++) {
cur_sum = 0;
for (int j = i; j < n; j++) {
cur_sum += arr[j];
// is current sum is prime
if (prime[cur_sum]) {
max_sum = Math.max(max_sum, j - i + 1);
}
}
}
// return maximum sum founded.
return max_sum;
}
// Driver Code
public static void main(String[] args)
{
int n = 6;
int []arr = { 5, 2, 11, 4, 7, 19 };
System.out.print(maxLenSubarrayWithSumPrime(arr, n));
}
}
// This code is contributed by shikhasingrajput.
Python3
# Python 3 program for above approach
from math import sqrt
# Utility function to find whether number is
# prime or not
def SieveOfEratosthenes(prime, total_sum):
# Create a boolean array "prime[0..n]" and
# initialize all entries it as true.
# A value in prime[i] will finally be false
# if i is Not a prime, else true.
for i in range(total_sum+1):
prime[i] = True
for p in range(2,int(sqrt(total_sum)),1):
# If prime[p] is not changed,
# then it is a prime
if (prime[p] == True):
# Update all multiples of p
# greater than or equal to the square
# of it numbers which are multiple of p
# and are less than p^2 are already
# been marked.
for i in range(p * p,total_sum+1,p):
prime[i] = False
def maxLenSubarrayWithSumPrime(arr, n):
# to store total_sum of original array
total_sum = 0
# calculate total_sum
for i in range(n):
total_sum += arr[i]
# to store whether the number is
# prime or not
prime = [ False for i in range(total_sum + 1)]
# calling sieve to get prime values
SieveOfEratosthenes(prime, total_sum)
# to keep track of current and
# maximum sum till now
max_sum = 0
cur_sum = 0
for i in range(n):
cur_sum = 0
for j in range(i,n,1):
cur_sum += arr[j]
# is current sum is prime
if (prime[cur_sum]):
max_sum = max(max_sum, j - i + 1)
# return maximum sum founded.
return max_sum
# Driver Code
if __name__ == '__main__':
n = 6
arr = [5, 2, 11, 4, 7, 19]
print(maxLenSubarrayWithSumPrime(arr, n))
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for above approach
using System;
class GFG {
// Utility function to find whether number is
// prime or not
static void SieveOfEratosthenes(bool[] prime,
int total_sum)
{
// Create a boolean array "prime[0..n]" and
// initialize all entries it as true.
// A value in prime[i] will finally be false
// if i is Not a prime, else true.
for (int i = 0; i <= total_sum; i++) {
prime[i] = true;
}
for (int p = 2; p * p <= total_sum; p++) {
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true) {
// Update all multiples of p
// greater than or equal to the square
// of it numbers which are multiple of p
// and are less than p^2 are already
// been marked.
for (int i = p * p; i <= total_sum; i += p)
prime[i] = false;
}
}
}
static int maxLenSubarrayWithSumPrime(int[] arr, int n)
{
// to store total_sum of original array
int total_sum = 0;
// calculate total_sum
for (int i = 0; i < n; i++) {
total_sum += arr[i];
}
// to store whether the number is
// prime or not
bool[] prime = new bool[total_sum + 1];
// calling sieve to get prime values
SieveOfEratosthenes(prime, total_sum);
// to keep track of current and
// maximum sum till now
int max_sum = 0, cur_sum = 0;
for (int i = 0; i < n; i++) {
cur_sum = 0;
for (int j = i; j < n; j++) {
cur_sum += arr[j];
// is current sum is prime
if (prime[cur_sum]) {
max_sum = Math.Max(max_sum, j - i + 1);
}
}
}
// return maximum sum founded.
return max_sum;
}
// Driver Code
public static void Main(string[] args)
{
int n = 6;
int[] arr = { 5, 2, 11, 4, 7, 19 };
Console.WriteLine(
maxLenSubarrayWithSumPrime(arr, n));
}
}
// This code is contributed by ukasp.
Javascript
输出
5
时间复杂度: O(N^2)
辅助复杂度: O(N)