删除一个元素后的最长素数子数组
给定一个整数数组 A。我们最多可以从数组中删除一个索引。我们的目标是最大化包含所有素数的子数组的长度。打印通过从数组中删除一个元素可以实现的最大长度子数组。
例子:
Input : arr[] = { 2, 8, 5, 7, 9, 5, 7 }
Output : 4
Explanation :If we remove the number 9 which is at index 5 then the remaining array contains a subarray whose length is 4 which is maximum.
Input : arr[] = { 2, 3, 5, 7 }
Output : 3
If we remove the number 3 which is at index 1 then the remaining array contains a subarray whose length is 3 which is maximum.
这个想法是在每个索引之前和每个索引之后计算连续的素数。现在再次遍历数组并找到一个索引,其中前后素数之和最大。
C++
// CPP program to find length of the longest
// subarray with all primes except possibly
// one.
#include
using namespace std;
#define N 100000
bool prime[N];
void SieveOfEratosthenes()
{
// 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.
memset(prime, true, sizeof(prime));
for (int p = 2; p * p <= N; p++) {
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * 2; i <= N; i += p)
prime[i] = false;
}
}
}
int longestPrimeSubarray(int arr[], int n)
{
int left[n], right[n];
int primecount = 0, res = 0;
// left array used to count number of
// continuous prime numbers starting
// from left of current element
for (int i = 0; i < n; i++) {
left[i] = primecount;
if (prime[arr[i]]) {
primecount++;
}
else
primecount = 0;
}
// right array used to count number of
// continuous prime numbers starting from
// right of current element
primecount = 0;
for (int i = n - 1; i >= 0; i--) {
right[i] = primecount;
if (prime[arr[i]]) {
primecount++;
}
else
primecount = 0;
}
for (int i = 0; i < n; i++)
res = max(res, left[i] + right[i]);
return res;
}
// Driver code
int main()
{
int arr[] = { 2, 8, 5, 7, 9, 5, 7 };
// used of SieveOfEratosthenes method to
// detect a number prime or not
SieveOfEratosthenes();
int n = sizeof(arr) / sizeof(arr[0]);
cout << "largest length of PrimeSubarray "
<< longestPrimeSubarray(arr, n) << endl;
return 0;
}
Java
// Java program to find length of the longest
// subarray with all primes except possibly
// one.
import java.util.*;
class GFG
{
static int N = 100000;
static boolean prime[] = new boolean[N];
static void SieveOfEratosthenes()
{
// 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.
Arrays.fill(prime,true);
for (int p = 2; p * p <= N; p++)
{
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p * 2; i < N; i += p)
prime[i] = false;
}
}
}
static int longestPrimeSubarray(int arr[], int n)
{
int []left = new int[n];int[] right = new int[n];
int primecount = 0, res = 0;
// left array used to count number of
// continuous prime numbers starting
// from left of current element
for (int i = 0; i < n; i++)
{
left[i] = primecount;
if (prime[arr[i]])
{
primecount++;
}
else
primecount = 0;
}
// right array used to count number of
// continuous prime numbers starting from
// right of current element
primecount = 0;
for (int i = n - 1; i >= 0; i--)
{
right[i] = primecount;
if (prime[arr[i]])
{
primecount++;
}
else
primecount = 0;
}
for (int i = 0; i < n; i++)
res = Math.max(res, left[i] + right[i]);
return res;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 2, 8, 5, 7, 9, 5, 7 };
// used of SieveOfEratosthenes method to
// detect a number prime or not
SieveOfEratosthenes();
int n = arr.length;
System.out.println("largest length of PrimeSubarray "
+ longestPrimeSubarray(arr, n));
}
}
// This code contributed by Rajput-Ji
Python3
# Python 3 program to find length of the
# longest subarray with all primes except
# possibly one.
from math import sqrt
N = 100000
prime = [True for i in range(N + 1)]
def SieveOfEratosthenes():
# 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.
k = int(sqrt(N)) + 1
for p in range(2, k, 1):
# If prime[p] is not changed,
# then it is a prime
if (prime[p] == True):
# Update all multiples of p
for i in range(p * 2, N + 1, p):
prime[i] = False
def longestPrimeSubarray(arr, n):
left = [0 for i in range(n)]
right = [0 for i in range(n)]
primecount = 0
res = 0
# left array used to count number of
# continuous prime numbers starting
# from left of current element
for i in range(n):
left[i] = primecount
if (prime[arr[i]]):
primecount += 1
else:
primecount = 0
# right array used to count number of
# continuous prime numbers starting
# from right of current element
primecount = 0
i = n - 1
while(i >= 0):
right[i] = primecount
if (prime[arr[i]]):
primecount += 1
else:
primecount = 0
i -= 1
for i in range(n):
res = max(res, left[i] + right[i])
return res
# Driver code
if __name__ == '__main__':
arr = [2, 8, 5, 7, 9, 5, 7]
# used of SieveOfEratosthenes method
# to detect a number prime or not
SieveOfEratosthenes()
n = len(arr)
print("largest length of PrimeSubarray",
longestPrimeSubarray(arr, n))
# This code is contributed by
# Surendra_Gangwar
C#
// C# program to find length of the longest
// subarray with all primes except possibly
// one.
using System;
class GFG
{
static int N = 100000;
static bool []prime = new bool[N];
static void SieveOfEratosthenes()
{
// 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= 0; i--)
{
right[i] = primecount;
if (prime[arr[i]])
{
primecount++;
}
else
primecount = 0;
}
for (int i = 0; i < n; i++)
res = Math.Max(res, left[i] + right[i]);
return res;
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 2, 8, 5, 7, 9, 5, 7 };
// used of SieveOfEratosthenes method to
// detect a number prime or not
SieveOfEratosthenes();
int n = arr.Length;
Console.WriteLine("largest length of PrimeSubarray "
+ longestPrimeSubarray(arr, n));
}
}
// This code has been contributed by 29AjayKumar
PHP
= 0; $i--)
{
$right[$i] = $primecount;
if ($prime[$arr[$i]])
{
$primecount++;
}
else
$primecount = 0;
}
for ($i = 0; $i < $n; $i++)
$res = max($res, $left[$i] +
$right[$i]);
return $res;
}
// Driver Code
$arr = array(2, 8, 5, 7, 9, 5, 7);
// used of SieveOfEratosthenes method
// to detect a number prime or not
SieveOfEratosthenes();
$n = count($arr);
echo "largest length of PrimeSubarray " .
longestPrimeSubarray($arr, $n);
// This code is contributed by mits
?>
Javascript
输出:
largest length of PrimeSubarray 4